blob: 522b719803c34061a35375b88467ce6001296417 [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"
Michael J. Spencerab425d82010-11-29 18:47:54 +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;
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 Lattner77d369c2010-12-13 00:23:57 +000060class RecordKeeper;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000061
62//===----------------------------------------------------------------------===//
63// Type Classes
64//===----------------------------------------------------------------------===//
65
66struct RecTy {
67 virtual ~RecTy() {}
68
Chris Lattner8b9ecda2007-11-20 22:25:16 +000069 virtual std::string getAsString() const = 0;
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000070 void print(raw_ostream &OS) const { OS << getAsString(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000071 void dump() const;
72
73 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
74 /// converted to the specified type.
75 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
76
77public: // These methods should only be called from subclasses of Init
78 virtual Init *convertValue( UnsetInit *UI) { return 0; }
79 virtual Init *convertValue( BitInit *BI) { return 0; }
80 virtual Init *convertValue( BitsInit *BI) { return 0; }
81 virtual Init *convertValue( IntInit *II) { return 0; }
82 virtual Init *convertValue(StringInit *SI) { return 0; }
83 virtual Init *convertValue( ListInit *LI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +000084 virtual Init *convertValue( UnOpInit *UI) {
85 return convertValue((TypedInit*)UI);
86 }
David Greene196ac3c2009-04-23 21:25:15 +000087 virtual Init *convertValue( BinOpInit *UI) {
88 return convertValue((TypedInit*)UI);
89 }
David Greene98ed3c72009-05-14 21:54:42 +000090 virtual Init *convertValue( TernOpInit *UI) {
91 return convertValue((TypedInit*)UI);
92 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000093 virtual Init *convertValue( CodeInit *CI) { return 0; }
94 virtual Init *convertValue(VarBitInit *VB) { return 0; }
95 virtual Init *convertValue( DefInit *DI) { return 0; }
96 virtual Init *convertValue( DagInit *DI) { return 0; }
97 virtual Init *convertValue( TypedInit *TI) { return 0; }
98 virtual Init *convertValue( VarInit *VI) {
99 return convertValue((TypedInit*)VI);
100 }
101 virtual Init *convertValue( FieldInit *FI) {
102 return convertValue((TypedInit*)FI);
103 }
104
105public: // These methods should only be called by subclasses of RecTy.
106 // baseClassOf - These virtual methods should be overloaded to return true iff
107 // all values of type 'RHS' can be converted to the 'this' type.
108 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
109 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
110 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
114 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
115 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
116};
117
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000118inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000119 Ty.print(OS);
120 return OS;
121}
122
123
124/// BitRecTy - 'bit' - Represent a single bit
125///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000126class BitRecTy : public RecTy {
127public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000128 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
129 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
130 virtual Init *convertValue( BitsInit *BI);
131 virtual Init *convertValue( IntInit *II);
132 virtual Init *convertValue(StringInit *SI) { return 0; }
133 virtual Init *convertValue( ListInit *LI) { return 0; }
134 virtual Init *convertValue( CodeInit *CI) { return 0; }
135 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
136 virtual Init *convertValue( DefInit *DI) { return 0; }
137 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000138 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000139 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000140 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000141 virtual Init *convertValue( TypedInit *TI);
142 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
143 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000144
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000145 std::string getAsString() const { return "bit"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000146
147 bool typeIsConvertibleTo(const RecTy *RHS) const {
148 return RHS->baseClassOf(this);
149 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000150 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
151 virtual bool baseClassOf(const BitsRecTy *RHS) const;
152 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
153 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
154 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
155 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
156 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
157 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
158
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000159};
160
161
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000162// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
163/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000164///
165class BitsRecTy : public RecTy {
166 unsigned Size;
167public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000168 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000169
170 unsigned getNumBits() const { return Size; }
171
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000172 virtual Init *convertValue( UnsetInit *UI);
173 virtual Init *convertValue( BitInit *UI);
174 virtual Init *convertValue( BitsInit *BI);
175 virtual Init *convertValue( IntInit *II);
176 virtual Init *convertValue(StringInit *SI) { return 0; }
177 virtual Init *convertValue( ListInit *LI) { return 0; }
178 virtual Init *convertValue( CodeInit *CI) { return 0; }
179 virtual Init *convertValue(VarBitInit *VB) { return 0; }
180 virtual Init *convertValue( DefInit *DI) { return 0; }
181 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000182 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000183 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000184 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000185 virtual Init *convertValue( TypedInit *TI);
186 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
187 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
188
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000189 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000190
191 bool typeIsConvertibleTo(const RecTy *RHS) const {
192 return RHS->baseClassOf(this);
193 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000194 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
195 virtual bool baseClassOf(const BitsRecTy *RHS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000196 return RHS->Size == Size;
197 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000198 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
199 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
200 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
201 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
202 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
203 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
204
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000205};
206
207
208/// IntRecTy - 'int' - Represent an integer value of no particular size
209///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000210class IntRecTy : public RecTy {
211public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000212 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
213 virtual Init *convertValue( BitInit *BI);
214 virtual Init *convertValue( BitsInit *BI);
215 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
216 virtual Init *convertValue(StringInit *SI) { return 0; }
217 virtual Init *convertValue( ListInit *LI) { return 0; }
218 virtual Init *convertValue( CodeInit *CI) { return 0; }
219 virtual Init *convertValue(VarBitInit *VB) { return 0; }
220 virtual Init *convertValue( DefInit *DI) { return 0; }
221 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000222 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000223 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000224 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000225 virtual Init *convertValue( TypedInit *TI);
226 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
227 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
228
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000229 std::string getAsString() const { return "int"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000230
231 bool typeIsConvertibleTo(const RecTy *RHS) const {
232 return RHS->baseClassOf(this);
233 }
234
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000235 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
236 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
237 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
238 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
241 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
242 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
243
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000244};
245
246/// StringRecTy - 'string' - Represent an string value
247///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000248class StringRecTy : public RecTy {
249public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000250 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
251 virtual Init *convertValue( BitInit *BI) { return 0; }
252 virtual Init *convertValue( BitsInit *BI) { return 0; }
253 virtual Init *convertValue( IntInit *II) { return 0; }
254 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
255 virtual Init *convertValue( ListInit *LI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000256 virtual Init *convertValue( UnOpInit *BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000257 virtual Init *convertValue( BinOpInit *BO);
David Greene98ed3c72009-05-14 21:54:42 +0000258 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
David Greene5d0c0512009-05-14 20:54:48 +0000259
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000260 virtual Init *convertValue( CodeInit *CI) { return 0; }
261 virtual Init *convertValue(VarBitInit *VB) { return 0; }
262 virtual Init *convertValue( DefInit *DI) { return 0; }
263 virtual Init *convertValue( DagInit *DI) { return 0; }
264 virtual Init *convertValue( TypedInit *TI);
265 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
266 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
267
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000268 std::string getAsString() const { return "string"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000269
270 bool typeIsConvertibleTo(const RecTy *RHS) const {
271 return RHS->baseClassOf(this);
272 }
273
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000274 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
275 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
276 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000277 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000278 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
279 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
280 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
281 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000282};
283
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000284// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
285// the specified type.
286/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
287/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000288///
289class ListRecTy : public RecTy {
290 RecTy *Ty;
291public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000292 explicit ListRecTy(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000293
294 RecTy *getElementType() const { return Ty; }
295
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000296 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
297 virtual Init *convertValue( BitInit *BI) { return 0; }
298 virtual Init *convertValue( BitsInit *BI) { return 0; }
299 virtual Init *convertValue( IntInit *II) { return 0; }
300 virtual Init *convertValue(StringInit *SI) { return 0; }
301 virtual Init *convertValue( ListInit *LI);
302 virtual Init *convertValue( CodeInit *CI) { return 0; }
303 virtual Init *convertValue(VarBitInit *VB) { return 0; }
304 virtual Init *convertValue( DefInit *DI) { return 0; }
305 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000306 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000307 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000308 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000309 virtual Init *convertValue( TypedInit *TI);
310 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
311 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000312
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000313 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000314
315 bool typeIsConvertibleTo(const RecTy *RHS) const {
316 return RHS->baseClassOf(this);
317 }
318
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000319 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
320 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
321 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
322 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
323 virtual bool baseClassOf(const ListRecTy *RHS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000324 return RHS->getElementType()->typeIsConvertibleTo(Ty);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000325 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000326 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
328 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000329};
330
331/// CodeRecTy - 'code' - Represent an code fragment, function or method.
332///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000333class CodeRecTy : public RecTy {
334public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000335 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
336 virtual Init *convertValue( BitInit *BI) { return 0; }
337 virtual Init *convertValue( BitsInit *BI) { return 0; }
338 virtual Init *convertValue( IntInit *II) { return 0; }
339 virtual Init *convertValue(StringInit *SI) { return 0; }
340 virtual Init *convertValue( ListInit *LI) { return 0; }
341 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
342 virtual Init *convertValue(VarBitInit *VB) { return 0; }
343 virtual Init *convertValue( DefInit *DI) { return 0; }
344 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000345 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000346 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000347 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000348 virtual Init *convertValue( TypedInit *TI);
349 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
350 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
351
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000352 std::string getAsString() const { return "code"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000353
354 bool typeIsConvertibleTo(const RecTy *RHS) const {
355 return RHS->baseClassOf(this);
356 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000357 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
358 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
359 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
360 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
361 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
362 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
363 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
364 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000365};
366
367/// DagRecTy - 'dag' - Represent a dag fragment
368///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000369class DagRecTy : public RecTy {
370public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000371 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
372 virtual Init *convertValue( BitInit *BI) { return 0; }
373 virtual Init *convertValue( BitsInit *BI) { return 0; }
374 virtual Init *convertValue( IntInit *II) { return 0; }
375 virtual Init *convertValue(StringInit *SI) { return 0; }
376 virtual Init *convertValue( ListInit *LI) { return 0; }
377 virtual Init *convertValue( CodeInit *CI) { return 0; }
378 virtual Init *convertValue(VarBitInit *VB) { return 0; }
379 virtual Init *convertValue( DefInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000380 virtual Init *convertValue( UnOpInit *BO);
Evan Chenga32dee22007-05-15 01:23:24 +0000381 virtual Init *convertValue( BinOpInit *BO);
David Greene98ed3c72009-05-14 21:54:42 +0000382 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000383 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
384 virtual Init *convertValue( TypedInit *TI);
385 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
386 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000387
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000388 std::string getAsString() const { return "dag"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000389
390 bool typeIsConvertibleTo(const RecTy *RHS) const {
391 return RHS->baseClassOf(this);
392 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000393
394 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
395 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
396 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
397 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
398 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
399 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
400 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
401 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000402};
403
404
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000405/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000406/// (R32 X = EAX).
407///
408class RecordRecTy : public RecTy {
409 Record *Rec;
410public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000411 explicit RecordRecTy(Record *R) : Rec(R) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000412
413 Record *getRecord() const { return Rec; }
414
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000415 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
416 virtual Init *convertValue( BitInit *BI) { return 0; }
417 virtual Init *convertValue( BitsInit *BI) { return 0; }
418 virtual Init *convertValue( IntInit *II) { return 0; }
419 virtual Init *convertValue(StringInit *SI) { return 0; }
420 virtual Init *convertValue( ListInit *LI) { return 0; }
421 virtual Init *convertValue( CodeInit *CI) { return 0; }
422 virtual Init *convertValue(VarBitInit *VB) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000423 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000424 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000425 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000426 virtual Init *convertValue( DefInit *DI);
427 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000428 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000429 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
430 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000431
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000432 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000433
434 bool typeIsConvertibleTo(const RecTy *RHS) const {
435 return RHS->baseClassOf(this);
436 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000437 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
438 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
439 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
440 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
441 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
442 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
443 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000444 virtual bool baseClassOf(const RecordRecTy *RHS) const;
445};
446
Bob Wilson7248f862009-11-22 04:24:42 +0000447/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000448/// Return 0 if no such type exists.
449///
450RecTy *resolveTypes(RecTy *T1, RecTy *T2);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000451
452//===----------------------------------------------------------------------===//
453// Initializer Classes
454//===----------------------------------------------------------------------===//
455
456struct Init {
457 virtual ~Init() {}
458
459 /// isComplete - This virtual method should be overridden by values that may
460 /// not be completely specified yet.
461 virtual bool isComplete() const { return true; }
462
463 /// print - Print out this value.
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000464 void print(raw_ostream &OS) const { OS << getAsString(); }
Chris Lattner695506c2007-11-22 21:05:25 +0000465
466 /// getAsString - Convert this value to a string form.
467 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000468
469 /// dump - Debugging method that may be called through a debugger, just
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000470 /// invokes print on stderr.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000471 void dump() const;
472
473 /// convertInitializerTo - This virtual function is a simple call-back
474 /// function that should be overridden to call the appropriate
475 /// RecTy::convertValue method.
476 ///
477 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
478
479 /// convertInitializerBitRange - This method is used to implement the bitrange
480 /// selection operator. Given an initializer, it selects the specified bits
481 /// out, returning them as a new init of bits type. If it is not legal to use
482 /// the bit subscript operator on this initializer, return null.
483 ///
484 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
485 return 0;
486 }
487
Chris Lattner8bf9e062004-07-26 23:21:34 +0000488 /// convertInitListSlice - This method is used to implement the list slice
489 /// selection operator. Given an initializer, it selects the specified list
490 /// elements, returning them as a new init of list type. If it is not legal
491 /// to take a slice of this, return null.
492 ///
493 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
494 return 0;
495 }
496
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000497 /// getFieldType - This method is used to implement the FieldInit class.
498 /// Implementors of this method should return the type of the named field if
499 /// they are of record type.
500 ///
501 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
502
503 /// getFieldInit - This method complements getFieldType to return the
504 /// initializer for the specified field. If getFieldType returns non-null
505 /// this method should return non-null, otherwise it returns null.
506 ///
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +0000507 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
508 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000509 return 0;
510 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000511
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000512 /// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +0000513 /// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000514 /// If a value is set for the variable later, this method will be called on
515 /// users of the value to allow the value to propagate out.
516 ///
Chris Lattneref943742005-04-19 03:36:21 +0000517 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
518 return this;
519 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000520};
521
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000522inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000523 I.print(OS); return OS;
524}
525
David Greenee917fff2009-05-14 22:23:47 +0000526/// TypedInit - This is the common super-class of types that have a specific,
527/// explicit, type.
528///
529class TypedInit : public Init {
530 RecTy *Ty;
531public:
532 explicit TypedInit(RecTy *T) : Ty(T) {}
533
534 RecTy *getType() const { return Ty; }
535
536 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
537 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
538
David Greene2a9de4d2010-09-03 21:00:49 +0000539 /// getFieldType - This method is used to implement the FieldInit class.
540 /// Implementors of this method should return the type of the named field if
541 /// they are of record type.
542 ///
543 virtual RecTy *getFieldType(const std::string &FieldName) const;
544
David Greenee917fff2009-05-14 22:23:47 +0000545 /// resolveBitReference - This method is used to implement
546 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
547 /// simply return the resolved value, otherwise we return null.
548 ///
549 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
550 unsigned Bit) = 0;
551
552 /// resolveListElementReference - This method is used to implement
553 /// VarListElementInit::resolveReferences. If the list element is resolvable
554 /// now, we return the resolved value, otherwise we return null.
555 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
556 unsigned Elt) = 0;
557};
558
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000559
560/// UnsetInit - ? - Represents an uninitialized value
561///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000562class UnsetInit : public Init {
563public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000564 virtual Init *convertInitializerTo(RecTy *Ty) {
565 return Ty->convertValue(this);
566 }
567
568 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000569 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000570};
571
572
573/// BitInit - true/false - Represent a concrete initializer for a bit.
574///
575class BitInit : public Init {
576 bool Value;
577public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000578 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000579
580 bool getValue() const { return Value; }
581
582 virtual Init *convertInitializerTo(RecTy *Ty) {
583 return Ty->convertValue(this);
584 }
585
Chris Lattner695506c2007-11-22 21:05:25 +0000586 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000587};
588
589/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
590/// It contains a vector of bits, whose size is determined by the type.
591///
592class BitsInit : public Init {
593 std::vector<Init*> Bits;
594public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000595 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000596
597 unsigned getNumBits() const { return Bits.size(); }
598
599 Init *getBit(unsigned Bit) const {
600 assert(Bit < Bits.size() && "Bit index out of range!");
601 return Bits[Bit];
602 }
603 void setBit(unsigned Bit, Init *V) {
604 assert(Bit < Bits.size() && "Bit index out of range!");
605 assert(Bits[Bit] == 0 && "Bit already set!");
606 Bits[Bit] = V;
607 }
608
609 virtual Init *convertInitializerTo(RecTy *Ty) {
610 return Ty->convertValue(this);
611 }
612 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
613
614 virtual bool isComplete() const {
615 for (unsigned i = 0; i != getNumBits(); ++i)
616 if (!getBit(i)->isComplete()) return false;
617 return true;
618 }
Johnny Chen4e8bd582010-04-09 21:01:02 +0000619 bool allInComplete() const {
620 for (unsigned i = 0; i != getNumBits(); ++i)
621 if (getBit(i)->isComplete()) return false;
622 return true;
623 }
Chris Lattner695506c2007-11-22 21:05:25 +0000624 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000625
Chris Lattneref943742005-04-19 03:36:21 +0000626 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000627};
628
629
630/// IntInit - 7 - Represent an initalization by a literal integer value.
631///
David Greene8618f952009-06-08 20:23:18 +0000632class IntInit : public TypedInit {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000633 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000634public:
David Greene8618f952009-06-08 20:23:18 +0000635 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000636
Dan Gohmanca0546f2008-10-17 01:33:43 +0000637 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000638
639 virtual Init *convertInitializerTo(RecTy *Ty) {
640 return Ty->convertValue(this);
641 }
642 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
643
Chris Lattner695506c2007-11-22 21:05:25 +0000644 virtual std::string getAsString() const;
David Greene8618f952009-06-08 20:23:18 +0000645
646 /// resolveBitReference - This method is used to implement
647 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
648 /// simply return the resolved value, otherwise we return null.
649 ///
650 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
651 unsigned Bit) {
652 assert(0 && "Illegal bit reference off int");
653 return 0;
654 }
655
656 /// resolveListElementReference - This method is used to implement
657 /// VarListElementInit::resolveReferences. If the list element is resolvable
658 /// now, we return the resolved value, otherwise we return null.
659 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
660 unsigned Elt) {
661 assert(0 && "Illegal element reference off int");
662 return 0;
663 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000664};
665
666
667/// StringInit - "foo" - Represent an initialization by a string value.
668///
David Greenee917fff2009-05-14 22:23:47 +0000669class StringInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000670 std::string Value;
671public:
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000672 explicit StringInit(const std::string &V)
673 : TypedInit(new StringRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000674
675 const std::string &getValue() const { return Value; }
676
677 virtual Init *convertInitializerTo(RecTy *Ty) {
678 return Ty->convertValue(this);
679 }
680
Chris Lattner695506c2007-11-22 21:05:25 +0000681 virtual std::string getAsString() const { return "\"" + Value + "\""; }
David Greenee917fff2009-05-14 22:23:47 +0000682
683 /// resolveBitReference - This method is used to implement
684 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
685 /// simply return the resolved value, otherwise we return null.
686 ///
687 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
688 unsigned Bit) {
689 assert(0 && "Illegal bit reference off string");
690 return 0;
691 }
692
693 /// resolveListElementReference - This method is used to implement
694 /// VarListElementInit::resolveReferences. If the list element is resolvable
695 /// now, we return the resolved value, otherwise we return null.
696 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
697 unsigned Elt) {
698 assert(0 && "Illegal element reference off string");
699 return 0;
700 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000701};
702
703/// CodeInit - "[{...}]" - Represent a code fragment.
704///
705class CodeInit : public Init {
706 std::string Value;
707public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000708 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000709
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000710 const std::string &getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000711
712 virtual Init *convertInitializerTo(RecTy *Ty) {
713 return Ty->convertValue(this);
714 }
715
Chris Lattner695506c2007-11-22 21:05:25 +0000716 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000717};
718
719/// ListInit - [AL, AH, CL] - Represent a list of defs
720///
David Greene8618f952009-06-08 20:23:18 +0000721class ListInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000722 std::vector<Init*> Values;
723public:
David Greened571b3c2009-05-14 22:38:31 +0000724 typedef std::vector<Init*>::iterator iterator;
725 typedef std::vector<Init*>::const_iterator const_iterator;
726
David Greene8618f952009-06-08 20:23:18 +0000727 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
728 : TypedInit(new ListRecTy(EltTy)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000729 Values.swap(Vs);
730 }
David Greene8618f952009-06-08 20:23:18 +0000731 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
732 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000733
734 unsigned getSize() const { return Values.size(); }
735 Init *getElement(unsigned i) const {
736 assert(i < Values.size() && "List element index out of range!");
737 return Values[i];
738 }
739
Chris Lattnercbebe462007-02-27 22:08:27 +0000740 Record *getElementAsRecord(unsigned i) const;
Bob Wilson7248f862009-11-22 04:24:42 +0000741
Chris Lattner8bf9e062004-07-26 23:21:34 +0000742 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
743
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000744 virtual Init *convertInitializerTo(RecTy *Ty) {
745 return Ty->convertValue(this);
746 }
747
Chris Lattner577fc3f2004-07-27 01:01:21 +0000748 /// resolveReferences - This method is used by classes that refer to other
749 /// variables which may not be defined at the time they expression is formed.
750 /// If a value is set for the variable later, this method will be called on
751 /// users of the value to allow the value to propagate out.
752 ///
Chris Lattneref943742005-04-19 03:36:21 +0000753 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000754
Chris Lattner695506c2007-11-22 21:05:25 +0000755 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000756
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000757 inline iterator begin() { return Values.begin(); }
758 inline const_iterator begin() const { return Values.begin(); }
759 inline iterator end () { return Values.end(); }
760 inline const_iterator end () const { return Values.end(); }
761
762 inline size_t size () const { return Values.size(); }
763 inline bool empty() const { return Values.empty(); }
David Greene8618f952009-06-08 20:23:18 +0000764
765 /// resolveBitReference - This method is used to implement
766 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
767 /// simply return the resolved value, otherwise we return null.
768 ///
769 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
770 unsigned Bit) {
771 assert(0 && "Illegal bit reference off list");
772 return 0;
773 }
774
775 /// resolveListElementReference - This method is used to implement
776 /// VarListElementInit::resolveReferences. If the list element is resolvable
777 /// now, we return the resolved value, otherwise we return null.
778 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
779 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000780};
781
782
David Greene5d0c0512009-05-14 20:54:48 +0000783/// OpInit - Base class for operators
David Greene196ac3c2009-04-23 21:25:15 +0000784///
David Greene5d0c0512009-05-14 20:54:48 +0000785class OpInit : public TypedInit {
David Greene196ac3c2009-04-23 21:25:15 +0000786public:
David Greene5d0c0512009-05-14 20:54:48 +0000787 OpInit(RecTy *Type) : TypedInit(Type) {}
788
789 // Clone - Clone this operator, replacing arguments with the new list
790 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
791
Dan Gohman1432ef82009-08-12 22:10:57 +0000792 virtual int getNumOperands() const = 0;
David Greene5d0c0512009-05-14 20:54:48 +0000793 virtual Init *getOperand(int i) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000794
795 // Fold - If possible, fold this to a simpler init. Return this if not
796 // possible to fold.
David Greene5d0c0512009-05-14 20:54:48 +0000797 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000798
799 virtual Init *convertInitializerTo(RecTy *Ty) {
800 return Ty->convertValue(this);
801 }
Bob Wilson7248f862009-11-22 04:24:42 +0000802
David Greene196ac3c2009-04-23 21:25:15 +0000803 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
804 unsigned Bit);
805 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
806 unsigned Elt);
David Greene5d0c0512009-05-14 20:54:48 +0000807};
808
809
810/// UnOpInit - !op (X) - Transform an init.
811///
David Greenee8f3b272009-05-14 21:22:49 +0000812class UnOpInit : public OpInit {
813public:
David Greene2f7cf7f2011-01-07 17:05:37 +0000814 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
David Greenee8f3b272009-05-14 21:22:49 +0000815private:
816 UnaryOp Opc;
817 Init *LHS;
818public:
819 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
820 OpInit(Type), Opc(opc), LHS(lhs) {
821 }
David Greene5d0c0512009-05-14 20:54:48 +0000822
David Greenee8f3b272009-05-14 21:22:49 +0000823 // Clone - Clone this operator, replacing arguments with the new list
824 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000825 assert(Operands.size() == 1 &&
826 "Wrong number of operands for unary operation");
David Greenee8f3b272009-05-14 21:22:49 +0000827 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
828 }
David Greene5d0c0512009-05-14 20:54:48 +0000829
Dan Gohman1432ef82009-08-12 22:10:57 +0000830 int getNumOperands() const { return 1; }
David Greenee8f3b272009-05-14 21:22:49 +0000831 Init *getOperand(int i) {
832 assert(i == 0 && "Invalid operand id for unary operator");
833 return getOperand();
834 }
Bob Wilson7248f862009-11-22 04:24:42 +0000835
David Greenee8f3b272009-05-14 21:22:49 +0000836 UnaryOp getOpcode() const { return Opc; }
837 Init *getOperand() const { return LHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000838
David Greenee8f3b272009-05-14 21:22:49 +0000839 // Fold - If possible, fold this to a simpler init. Return this if not
840 // possible to fold.
841 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000842
David Greenee8f3b272009-05-14 21:22:49 +0000843 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000844
David Greenee8f3b272009-05-14 21:22:49 +0000845 virtual std::string getAsString() const;
846};
David Greene5d0c0512009-05-14 20:54:48 +0000847
848/// BinOpInit - !op (X, Y) - Combine two inits.
849///
850class BinOpInit : public OpInit {
851public:
Chris Lattner94026332010-10-06 00:19:21 +0000852 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
David Greene5d0c0512009-05-14 20:54:48 +0000853private:
854 BinaryOp Opc;
855 Init *LHS, *RHS;
856public:
857 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
858 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
859 }
Bob Wilson7248f862009-11-22 04:24:42 +0000860
David Greene5d0c0512009-05-14 20:54:48 +0000861 // Clone - Clone this operator, replacing arguments with the new list
862 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000863 assert(Operands.size() == 2 &&
864 "Wrong number of operands for binary operation");
David Greene5d0c0512009-05-14 20:54:48 +0000865 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
866 }
867
Dan Gohman1432ef82009-08-12 22:10:57 +0000868 int getNumOperands() const { return 2; }
David Greene5d0c0512009-05-14 20:54:48 +0000869 Init *getOperand(int i) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000870 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
David Greene5d0c0512009-05-14 20:54:48 +0000871 if (i == 0) {
872 return getLHS();
Bob Wilson7248f862009-11-22 04:24:42 +0000873 } else {
David Greene5d0c0512009-05-14 20:54:48 +0000874 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);
Bob Wilson7248f862009-11-22 04:24:42 +0000887
David Greene196ac3c2009-04-23 21:25:15 +0000888 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 }
Bob Wilson7248f862009-11-22 04:24:42 +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();
Bob Wilson7248f862009-11-22 04:24:42 +0000918 } else if (i == 1) {
David Greene98ed3c72009-05-14 21:54:42 +0000919 return getMHS();
Bob Wilson7248f862009-11-22 04:24:42 +0000920 } else {
David Greene98ed3c72009-05-14 21:54:42 +0000921 return getRHS();
922 }
923 }
David Greene5d0c0512009-05-14 20:54:48 +0000924
David Greene98ed3c72009-05-14 21:54:42 +0000925 TernaryOp getOpcode() const { return Opc; }
926 Init *getLHS() const { return LHS; }
927 Init *getMHS() const { return MHS; }
928 Init *getRHS() const { return RHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000929
David Greene98ed3c72009-05-14 21:54:42 +0000930 // Fold - If possible, fold this to a simpler init. Return this if not
931 // possible to fold.
932 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
Bob Wilson7248f862009-11-22 04:24:42 +0000933
Bill Wendling73ce4a62010-12-13 01:46:19 +0000934 virtual bool isComplete() const { return false; }
935
David Greene98ed3c72009-05-14 21:54:42 +0000936 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Bob Wilson7248f862009-11-22 04:24:42 +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;
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +0000962 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
963 const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000964
965 /// resolveReferences - This method is used by classes that refer to other
966 /// variables which may not be defined at the time they expression is formed.
967 /// If a value is set for the variable later, this method will be called on
968 /// users of the value to allow the value to propagate out.
969 ///
Chris Lattneref943742005-04-19 03:36:21 +0000970 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000971
Chris Lattner695506c2007-11-22 21:05:25 +0000972 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000973};
974
975
976/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
977///
978class VarBitInit : public Init {
979 TypedInit *TI;
980 unsigned Bit;
981public:
982 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
983 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
984 ((BitsRecTy*)T->getType())->getNumBits() > B &&
985 "Illegal VarBitInit expression!");
986 }
987
988 virtual Init *convertInitializerTo(RecTy *Ty) {
989 return Ty->convertValue(this);
990 }
991
992 TypedInit *getVariable() const { return TI; }
993 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000994
Chris Lattner695506c2007-11-22 21:05:25 +0000995 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000996 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000997};
998
Misha Brukman650ba8e2005-04-22 00:00:37 +0000999/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +00001000/// field.
1001class VarListElementInit : public TypedInit {
1002 TypedInit *TI;
1003 unsigned Element;
1004public:
1005 VarListElementInit(TypedInit *T, unsigned E)
1006 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1007 TI(T), Element(E) {
1008 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1009 "Illegal VarBitInit expression!");
1010 }
1011
1012 virtual Init *convertInitializerTo(RecTy *Ty) {
1013 return Ty->convertValue(this);
1014 }
1015
1016 TypedInit *getVariable() const { return TI; }
1017 unsigned getElementNum() const { return Element; }
1018
Chris Lattneref943742005-04-19 03:36:21 +00001019 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1020 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001021
1022 /// resolveListElementReference - This method is used to implement
1023 /// VarListElementInit::resolveReferences. If the list element is resolvable
1024 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +00001025 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1026 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001027
Chris Lattner695506c2007-11-22 21:05:25 +00001028 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +00001029 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001030};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001031
1032/// DefInit - AL - Represent a reference to a 'def' in the description
1033///
David Greenee917fff2009-05-14 22:23:47 +00001034class DefInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001035 Record *Def;
1036public:
David Greenee917fff2009-05-14 22:23:47 +00001037 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001038
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001039 virtual Init *convertInitializerTo(RecTy *Ty) {
1040 return Ty->convertValue(this);
1041 }
1042
1043 Record *getDef() const { return Def; }
1044
1045 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1046
1047 virtual RecTy *getFieldType(const std::string &FieldName) const;
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001048 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1049 const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001050
Chris Lattner695506c2007-11-22 21:05:25 +00001051 virtual std::string getAsString() const;
David Greenee917fff2009-05-14 22:23:47 +00001052
1053 /// resolveBitReference - This method is used to implement
1054 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1055 /// simply return the resolved value, otherwise we return null.
1056 ///
1057 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1058 unsigned Bit) {
1059 assert(0 && "Illegal bit reference off def");
1060 return 0;
1061 }
1062
1063 /// resolveListElementReference - This method is used to implement
1064 /// VarListElementInit::resolveReferences. If the list element is resolvable
1065 /// now, we return the resolved value, otherwise we return null.
1066 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1067 unsigned Elt) {
1068 assert(0 && "Illegal element reference off def");
1069 return 0;
1070 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001071};
1072
1073
1074/// FieldInit - X.Y - Represent a reference to a subfield of a variable
1075///
1076class FieldInit : public TypedInit {
1077 Init *Rec; // Record we are referring to
1078 std::string FieldName; // Field we are accessing
1079public:
1080 FieldInit(Init *R, const std::string &FN)
1081 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1082 assert(getType() && "FieldInit with non-record type!");
1083 }
1084
1085 virtual Init *convertInitializerTo(RecTy *Ty) {
1086 return Ty->convertValue(this);
1087 }
1088
Chris Lattneref943742005-04-19 03:36:21 +00001089 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1090 unsigned Bit);
1091 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1092 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001093
Chris Lattneref943742005-04-19 03:36:21 +00001094 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001095
Chris Lattner695506c2007-11-22 21:05:25 +00001096 virtual std::string getAsString() const {
1097 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001098 }
1099};
1100
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001101/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1102/// to have at least one value then a (possibly empty) list of arguments. Each
1103/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001104///
David Greenee917fff2009-05-14 22:23:47 +00001105class DagInit : public TypedInit {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001106 Init *Val;
Nate Begemandbe3f772009-03-19 05:21:56 +00001107 std::string ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001108 std::vector<Init*> Args;
1109 std::vector<std::string> ArgNames;
1110public:
Bob Wilson7248f862009-11-22 04:24:42 +00001111 DagInit(Init *V, std::string VN,
Nate Begemandbe3f772009-03-19 05:21:56 +00001112 const std::vector<std::pair<Init*, std::string> > &args)
David Greenee917fff2009-05-14 22:23:47 +00001113 : TypedInit(new DagRecTy), Val(V), ValName(VN) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001114 Args.reserve(args.size());
1115 ArgNames.reserve(args.size());
1116 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1117 Args.push_back(args[i].first);
1118 ArgNames.push_back(args[i].second);
1119 }
1120 }
Bob Wilson7248f862009-11-22 04:24:42 +00001121 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +00001122 const std::vector<std::string> &argNames)
Bob Wilson7248f862009-11-22 04:24:42 +00001123 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args),
1124 ArgNames(argNames) { }
1125
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001126 virtual Init *convertInitializerTo(RecTy *Ty) {
1127 return Ty->convertValue(this);
1128 }
1129
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001130 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001131
Nate Begemandbe3f772009-03-19 05:21:56 +00001132 const std::string &getName() const { return ValName; }
1133
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001134 unsigned getNumArgs() const { return Args.size(); }
1135 Init *getArg(unsigned Num) const {
1136 assert(Num < Args.size() && "Arg number out of range!");
1137 return Args[Num];
1138 }
1139 const std::string &getArgName(unsigned Num) const {
1140 assert(Num < ArgNames.size() && "Arg number out of range!");
1141 return ArgNames[Num];
1142 }
1143
1144 void setArg(unsigned Num, Init *I) {
1145 assert(Num < Args.size() && "Arg number out of range!");
1146 Args[Num] = I;
1147 }
Bob Wilson7248f862009-11-22 04:24:42 +00001148
Chris Lattner0d3ef402006-01-31 06:02:35 +00001149 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001150
Chris Lattner695506c2007-11-22 21:05:25 +00001151 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +00001152
1153 typedef std::vector<Init*>::iterator arg_iterator;
1154 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1155 typedef std::vector<std::string>::iterator name_iterator;
1156 typedef std::vector<std::string>::const_iterator const_name_iterator;
1157
1158 inline arg_iterator arg_begin() { return Args.begin(); }
1159 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1160 inline arg_iterator arg_end () { return Args.end(); }
1161 inline const_arg_iterator arg_end () const { return Args.end(); }
1162
1163 inline size_t arg_size () const { return Args.size(); }
1164 inline bool arg_empty() const { return Args.empty(); }
1165
1166 inline name_iterator name_begin() { return ArgNames.begin(); }
1167 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1168 inline name_iterator name_end () { return ArgNames.end(); }
1169 inline const_name_iterator name_end () const { return ArgNames.end(); }
1170
1171 inline size_t name_size () const { return ArgNames.size(); }
1172 inline bool name_empty() const { return ArgNames.empty(); }
1173
David Greenee917fff2009-05-14 22:23:47 +00001174 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1175 unsigned Bit) {
1176 assert(0 && "Illegal bit reference off dag");
1177 return 0;
1178 }
Bob Wilson7248f862009-11-22 04:24:42 +00001179
David Greenee917fff2009-05-14 22:23:47 +00001180 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1181 unsigned Elt) {
1182 assert(0 && "Illegal element reference off dag");
1183 return 0;
1184 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001185};
1186
1187//===----------------------------------------------------------------------===//
1188// High-Level Classes
1189//===----------------------------------------------------------------------===//
1190
1191class RecordVal {
1192 std::string Name;
1193 RecTy *Ty;
1194 unsigned Prefix;
1195 Init *Value;
1196public:
1197 RecordVal(const std::string &N, RecTy *T, unsigned P);
1198
1199 const std::string &getName() const { return Name; }
1200
1201 unsigned getPrefix() const { return Prefix; }
1202 RecTy *getType() const { return Ty; }
1203 Init *getValue() const { return Value; }
1204
1205 bool setValue(Init *V) {
1206 if (V) {
1207 Value = V->convertInitializerTo(Ty);
1208 return Value == 0;
1209 }
1210 Value = 0;
1211 return false;
1212 }
1213
1214 void dump() const;
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001215 void print(raw_ostream &OS, bool PrintSem = true) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001216};
1217
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001218inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001219 RV.print(OS << " ");
1220 return OS;
1221}
1222
Chris Lattner87a10612004-10-23 04:58:50 +00001223class Record {
Daniel Dunbarced00812009-08-23 09:47:37 +00001224 static unsigned LastID;
1225
1226 // Unique record ID.
1227 unsigned ID;
Chris Lattnerf9b2edb2005-08-19 17:58:49 +00001228 std::string Name;
Chris Lattner526c8cb2009-06-21 03:39:35 +00001229 SMLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001230 std::vector<std::string> TemplateArgs;
1231 std::vector<RecordVal> Values;
1232 std::vector<Record*> SuperClasses;
Chris Lattner77d369c2010-12-13 00:23:57 +00001233
1234 // Tracks Record instances. Not owned by Record.
1235 RecordKeeper &TrackedRecords;
1236
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001237public:
1238
Chris Lattner89dcb682010-12-15 04:48:22 +00001239 // Constructs a record.
1240 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
Chris Lattner77d369c2010-12-13 00:23:57 +00001241 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001242 ~Record() {}
Bob Wilson7248f862009-11-22 04:24:42 +00001243
Mikhail Glushenkov5be67642010-09-21 14:59:50 +00001244
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001245 static unsigned getNewUID() { return LastID++; }
Mikhail Glushenkov5be67642010-09-21 14:59:50 +00001246
1247
Daniel Dunbarced00812009-08-23 09:47:37 +00001248 unsigned getID() const { return ID; }
1249
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001250 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +00001251 void setName(const std::string &Name); // Also updates RecordKeeper.
Bob Wilson7248f862009-11-22 04:24:42 +00001252
Chris Lattner526c8cb2009-06-21 03:39:35 +00001253 SMLoc getLoc() const { return Loc; }
Bob Wilson7248f862009-11-22 04:24:42 +00001254
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001255 const std::vector<std::string> &getTemplateArgs() const {
1256 return TemplateArgs;
1257 }
1258 const std::vector<RecordVal> &getValues() const { return Values; }
1259 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1260
Chris Lattner42bb0c12009-09-18 18:31:37 +00001261 bool isTemplateArg(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001262 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1263 if (TemplateArgs[i] == Name) return true;
1264 return false;
1265 }
1266
Chris Lattner42bb0c12009-09-18 18:31:37 +00001267 const RecordVal *getValue(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001268 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1269 if (Values[i].getName() == Name) return &Values[i];
1270 return 0;
1271 }
Chris Lattner42bb0c12009-09-18 18:31:37 +00001272 RecordVal *getValue(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001273 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1274 if (Values[i].getName() == Name) return &Values[i];
1275 return 0;
1276 }
1277
Chris Lattner42bb0c12009-09-18 18:31:37 +00001278 void addTemplateArg(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001279 assert(!isTemplateArg(Name) && "Template arg already defined!");
1280 TemplateArgs.push_back(Name);
1281 }
1282
1283 void addValue(const RecordVal &RV) {
1284 assert(getValue(RV.getName()) == 0 && "Value already added!");
1285 Values.push_back(RV);
1286 }
1287
Chris Lattner42bb0c12009-09-18 18:31:37 +00001288 void removeValue(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001289 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1290 if (Values[i].getName() == Name) {
1291 Values.erase(Values.begin()+i);
1292 return;
1293 }
Bob Wilson3b793a42009-11-21 22:39:27 +00001294 assert(0 && "Cannot remove an entry that does not exist!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001295 }
1296
Ted Kremenek58e32872009-03-13 22:20:10 +00001297 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001298 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1299 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001300 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001301 return false;
1302 }
1303
Chris Lattner42bb0c12009-09-18 18:31:37 +00001304 bool isSubClassOf(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001305 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1306 if (SuperClasses[i]->getName() == Name)
1307 return true;
1308 return false;
1309 }
1310
1311 void addSuperClass(Record *R) {
1312 assert(!isSubClassOf(R) && "Already subclassing record!");
1313 SuperClasses.push_back(R);
1314 }
1315
Chris Lattneref943742005-04-19 03:36:21 +00001316 /// resolveReferences - If there are any field references that refer to fields
1317 /// that have been filled in, we can propagate the values now.
1318 ///
1319 void resolveReferences() { resolveReferencesTo(0); }
1320
1321 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1322 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1323 /// possible references.
1324 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001325
Chris Lattner77d369c2010-12-13 00:23:57 +00001326 RecordKeeper &getRecords() const {
Chris Lattner89dcb682010-12-15 04:48:22 +00001327 return TrackedRecords;
Chris Lattner77d369c2010-12-13 00:23:57 +00001328 }
1329
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001330 void dump() const;
1331
1332 //===--------------------------------------------------------------------===//
1333 // High-level methods useful to tablegen back-ends
1334 //
1335
1336 /// getValueInit - Return the initializer for a value with the specified name,
1337 /// or throw an exception if the field does not exist.
1338 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001339 Init *getValueInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001340
1341 /// getValueAsString - This method looks up the specified field and returns
1342 /// its value as a string, throwing an exception if the field does not exist
1343 /// or if the value is not a string.
1344 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001345 std::string getValueAsString(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001346
1347 /// getValueAsBitsInit - This method looks up the specified field and returns
1348 /// its value as a BitsInit, throwing an exception if the field does not exist
1349 /// or if the value is not the right type.
1350 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001351 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001352
1353 /// getValueAsListInit - This method looks up the specified field and returns
1354 /// its value as a ListInit, throwing an exception if the field does not exist
1355 /// or if the value is not the right type.
1356 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001357 ListInit *getValueAsListInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001358
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001359 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001360 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001361 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001362 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001363 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001364
Mikhail Glushenkov5be67642010-09-21 14:59:50 +00001365 /// getValueAsListOfInts - This method looks up the specified field and
1366 /// returns its value as a vector of integers, throwing an exception if the
1367 /// field does not exist or if the value is not the right type.
Anton Korobeynikova468a112007-11-11 11:19:37 +00001368 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001369 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
Bob Wilson7248f862009-11-22 04:24:42 +00001370
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001371 /// getValueAsDef - This method looks up the specified field and returns its
1372 /// value as a Record, throwing an exception if the field does not exist or if
1373 /// the value is not the right type.
1374 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001375 Record *getValueAsDef(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001376
1377 /// getValueAsBit - This method looks up the specified field and returns its
1378 /// value as a bit, 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 bool getValueAsBit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001382
1383 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001384 /// value as an int64_t, throwing an exception if the field does not exist or
1385 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001386 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001387 int64_t getValueAsInt(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001388
1389 /// getValueAsDag - This method looks up the specified field and returns its
1390 /// value as an Dag, throwing an exception if the field does not exist or if
1391 /// the value is not the right type.
1392 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001393 DagInit *getValueAsDag(StringRef FieldName) const;
Bob Wilson7248f862009-11-22 04:24:42 +00001394
Chris Lattnerae939eb2005-09-13 21:44:28 +00001395 /// getValueAsCode - This method looks up the specified field and returns
1396 /// its value as the string data in a CodeInit, throwing an exception if the
1397 /// field does not exist or if the value is not a code object.
1398 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001399 std::string getValueAsCode(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001400};
1401
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001402raw_ostream &operator<<(raw_ostream &OS, const Record &R);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001403
David Greenea9c6c5d2009-04-22 20:18:10 +00001404struct MultiClass {
1405 Record Rec; // Placeholder for template args and Name.
1406 typedef std::vector<Record*> RecordVector;
1407 RecordVector DefPrototypes;
David Greene7049e792009-04-24 16:55:41 +00001408
1409 void dump() const;
1410
Chris Lattner77d369c2010-12-13 00:23:57 +00001411 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1412 Rec(Name, Loc, Records) {}
David Greenea9c6c5d2009-04-22 20:18:10 +00001413};
1414
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001415class RecordKeeper {
1416 std::map<std::string, Record*> Classes, Defs;
1417public:
1418 ~RecordKeeper() {
1419 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001420 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001421 delete I->second;
1422 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001423 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001424 delete I->second;
1425 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001426
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001427 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1428 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1429
1430 Record *getClass(const std::string &Name) const {
1431 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1432 return I == Classes.end() ? 0 : I->second;
1433 }
1434 Record *getDef(const std::string &Name) const {
1435 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1436 return I == Defs.end() ? 0 : I->second;
1437 }
1438 void addClass(Record *R) {
1439 assert(getClass(R->getName()) == 0 && "Class already exists!");
1440 Classes.insert(std::make_pair(R->getName(), R));
1441 }
1442 void addDef(Record *R) {
1443 assert(getDef(R->getName()) == 0 && "Def already exists!");
1444 Defs.insert(std::make_pair(R->getName(), R));
1445 }
1446
Chris Lattnerac284252005-08-19 17:58:11 +00001447 /// removeClass - Remove, but do not delete, the specified record.
1448 ///
1449 void removeClass(const std::string &Name) {
1450 assert(Classes.count(Name) && "Class does not exist!");
1451 Classes.erase(Name);
1452 }
1453 /// removeDef - Remove, but do not delete, the specified record.
1454 ///
1455 void removeDef(const std::string &Name) {
1456 assert(Defs.count(Name) && "Def does not exist!");
1457 Defs.erase(Name);
1458 }
Bob Wilson7248f862009-11-22 04:24:42 +00001459
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001460 //===--------------------------------------------------------------------===//
1461 // High-level helper methods, useful for tablegen backends...
1462
1463 /// getAllDerivedDefinitions - This method returns all concrete definitions
1464 /// that derive from the specified class name. If a class with the specified
1465 /// name does not exist, an exception is thrown.
1466 std::vector<Record*>
1467 getAllDerivedDefinitions(const std::string &ClassName) const;
1468
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001469 void dump() const;
1470};
1471
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001472/// LessRecord - Sorting predicate to sort record pointers by name.
1473///
1474struct LessRecord {
1475 bool operator()(const Record *Rec1, const Record *Rec2) const {
Jakob Stoklund Olesend1d7ed62010-05-26 21:47:28 +00001476 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001477 }
1478};
1479
Bob Wilson7248f862009-11-22 04:24:42 +00001480/// LessRecordFieldName - Sorting predicate to sort record pointers by their
Jim Grosbach56938af2008-09-11 17:05:32 +00001481/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001482///
1483struct LessRecordFieldName {
1484 bool operator()(const Record *Rec1, const Record *Rec2) const {
1485 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1486 }
1487};
1488
Chris Lattnerba42e492009-03-13 16:25:21 +00001489
1490class TGError {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001491 SMLoc Loc;
Chris Lattnerba42e492009-03-13 16:25:21 +00001492 std::string Message;
1493public:
Chris Lattner526c8cb2009-06-21 03:39:35 +00001494 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
Bob Wilson7248f862009-11-22 04:24:42 +00001495
Chris Lattner526c8cb2009-06-21 03:39:35 +00001496 SMLoc getLoc() const { return Loc; }
Chris Lattnerba42e492009-03-13 16:25:21 +00001497 const std::string &getMessage() const { return Message; }
1498};
Bob Wilson7248f862009-11-22 04:24:42 +00001499
1500
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001501raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001502
Benjamin Kramerc7583112010-09-27 17:42:11 +00001503void PrintError(SMLoc ErrorLoc, const Twine &Msg);
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001504
Brian Gaeke960707c2003-11-11 22:41:34 +00001505} // End llvm namespace
1506
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001507#endif