OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/IceGlobalInits.h - Global initializers -------*- C++ -*-===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // This file declares the representation of global addresses | |
Jim Stichnoth
2014/10/03 01:28:08
Reformat? e.g. "and" ought to fit on this line.
Karl
2014/10/04 16:28:34
Done.
| |
11 // and initializers in Subzero. Global initializers are represented as | |
12 // a sequence of simple initializers. | |
13 // | |
14 //===----------------------------------------------------------------------===// | |
15 | |
16 #ifndef SUBZERO_SRC_ICEGLOBALINITS_H | |
17 #define SUBZERO_SRC_ICEGLOBALINITS_H | |
18 | |
19 #include "IceDefs.h" | |
20 | |
21 namespace llvm { | |
22 // TODO(kschimpf): Remove this dependency on LLVM IR. | |
23 class Value; | |
24 } | |
25 | |
26 namespace Ice { | |
27 | |
28 /// Models a global address, and its initializers. | |
29 class GlobalAddress { | |
30 public: | |
31 /// Base class for a global variable initializer. | |
32 class Initializer { | |
33 public: | |
34 /// Discriminator for LLVM-style RTTI. | |
35 enum InitializerKind { | |
36 DataInitializerKind, | |
37 ZeroInitializerKind, | |
38 RelocInitializerKind | |
39 }; | |
40 InitializerKind getKind() const { return Kind; } | |
41 virtual ~Initializer() {} | |
42 virtual SizeT getNumBytes() const = 0; | |
43 virtual void dump(Ostream &Stream) const = 0; | |
44 virtual void dumpType(Ostream &Stream) const; | |
45 | |
46 protected: | |
47 Initializer(InitializerKind Kind) : Kind(Kind) {} | |
48 | |
49 private: | |
50 const InitializerKind Kind; | |
51 }; | |
52 | |
53 // Models the data in a data initializer. | |
54 typedef std::vector<uint8_t> DataVecType; | |
55 | |
56 /// Defines a sequence of byte values as a data initializer. | |
57 class DataInitializer : public Initializer { | |
58 public: | |
59 template <class Data> | |
60 DataInitializer(const Data &Values) | |
61 : Initializer(DataInitializerKind), Contents(Values.size()) { | |
62 size_t i = 0; | |
63 for (auto &V : Values) { | |
64 Contents[i] = static_cast<uint8_t>(V); | |
jvoung (off chromium)
2014/10/03 16:15:29
Would it be cleaner to just:
Contents.push_back(..
Karl
2014/10/04 16:28:34
The reason I factored it this way was to optimize
| |
65 ++i; | |
66 } | |
67 } | |
68 DataInitializer(const char *Str, size_t StrLen) | |
69 : Initializer(DataInitializerKind), Contents(StrLen) { | |
70 for (size_t i = 0; i < StrLen; ++i) | |
71 Contents[i] = static_cast<uint8_t>(Str[i]); | |
72 } | |
73 ~DataInitializer() override {} | |
74 const DataVecType &getContents() const { return Contents; } | |
75 SizeT getNumBytes() const override { return Contents.size(); } | |
76 void dump(Ostream &Stream) const override; | |
77 static bool classof(const Initializer *D) { | |
78 return D->getKind() == DataInitializerKind; | |
79 } | |
80 | |
81 private: | |
82 // The byte contents of the data initializer. | |
83 DataVecType Contents; | |
jvoung (off chromium)
2014/10/03 16:15:30
Disallow copy/assign for these initializer types?
Karl
2014/10/04 16:28:34
Done.
| |
84 }; | |
85 | |
86 /// Defines a sequence of bytes initialized to zero. | |
87 class ZeroInitializer : public Initializer { | |
88 public: | |
89 ZeroInitializer(SizeT Size) | |
jvoung (off chromium)
2014/10/03 16:15:29
explicit
Karl
2014/10/04 16:28:34
Done.
| |
90 : Initializer(ZeroInitializerKind), Size(Size) {} | |
91 ~ZeroInitializer() override {} | |
92 SizeT getNumBytes() const override { return Size; } | |
93 void dump(Ostream &Stream) const override; | |
94 static bool classof(const Initializer *Z) { | |
95 return Z->getKind() == ZeroInitializerKind; | |
96 } | |
97 | |
98 private: | |
99 // The number of bytes to be zero initialized. | |
100 SizeT Size; | |
101 }; | |
102 | |
103 /// Defines the kind of relocation addresses allowed. | |
104 enum RelocationKind { FunctionRelocation, GlobalAddressRelocation }; | |
105 | |
106 /// Defines a relocation address (i.e. reference to a function | |
107 /// or global variable address). | |
108 struct RelocationAddress { | |
109 RelocationAddress(llvm::Value *Function) : Kind(FunctionRelocation) { | |
110 Address.Function = Function; | |
111 } | |
112 RelocationAddress(GlobalAddress *GlobalAddr) | |
113 : Kind(GlobalAddressRelocation) { | |
114 Address.GlobalAddr = GlobalAddr; | |
115 } | |
116 RelocationKind Kind; | |
117 union { | |
118 // TODO(kschimpf) Integrate Functions into ICE model. | |
119 llvm::Value *Function; | |
120 GlobalAddress *GlobalAddr; | |
121 } Address; | |
122 }; | |
123 | |
124 // Relocation address offsets must be 32 bit values. | |
125 typedef int32_t RelocOffsetType; | |
126 static const SizeT RelocAddrSize = 4; | |
127 | |
128 /// Defines the relocation value of another address. | |
129 class RelocInitializer : public Initializer { | |
130 public: | |
131 RelocInitializer(const RelocationAddress &Address, RelocOffsetType Offset) | |
132 : Initializer(RelocInitializerKind), Address(Address), Offset(Offset) {} | |
133 ~RelocInitializer() override {} | |
134 RelocOffsetType getOffset() const { return Offset; } | |
135 IceString getName() const; | |
136 SizeT getNumBytes() const override { return RelocAddrSize; } | |
137 void dump(Ostream &Stream) const override; | |
138 virtual void dumpType(Ostream &Stream) const; | |
139 static bool classof(const Initializer *R) { | |
140 return R->getKind() == RelocInitializerKind; | |
141 } | |
142 | |
143 private: | |
144 // The global address used in the relocation. | |
145 RelocationAddress Address; | |
146 // The offset to add to the relocation. | |
147 RelocOffsetType Offset; | |
148 }; | |
149 | |
150 /// Models the list of initializers. | |
151 typedef std::vector<Initializer *> InitializerListType; | |
152 | |
153 GlobalAddress() : Alignment(0), IsConstant(false), IsInternal(true) {} | |
154 ~GlobalAddress(); | |
155 | |
156 const InitializerListType &getInitializers() const { return Initializers; } | |
157 bool hasName() const { return !Name.empty(); } | |
158 const IceString &getName() const { return Name; } | |
159 void setName(const IceString &NewName) { Name = NewName; } | |
160 bool getIsConstant() const { return IsConstant; } | |
161 void setIsConstant(bool NewValue) { IsConstant = NewValue; } | |
162 uint32_t getAlignment() const { return Alignment; } | |
163 void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; } | |
164 bool getIsInternal() const { return IsInternal; } | |
165 | |
166 /// Returns the number of bytes for the initializer of the global | |
167 /// address. | |
168 SizeT getNumBytes() const { | |
169 SizeT Count = 0; | |
170 for (Initializer *Init : Initializers) { | |
171 Count += Init->getNumBytes(); | |
172 } | |
173 return Count; | |
174 } | |
175 | |
176 /// Addes Initializer to the list of initializers. Takes ownership of | |
jvoung (off chromium)
2014/10/03 16:15:30
Adds
Karl
2014/10/04 16:28:34
Done.
| |
177 /// the initializer. | |
178 void AddInitializer(Initializer *Initializer) { | |
jvoung (off chromium)
2014/10/03 16:15:30
naming style: addInitializer(...) ?
Karl
2014/10/04 16:28:34
Done.
| |
179 Initializers.push_back(Initializer); | |
180 } | |
181 | |
182 /// Prints out type for initializer associated with the global address | |
183 /// to Stream. | |
184 void dumpType(Ostream &Stream) const; | |
185 | |
186 /// Prints out the definition of the global address (including | |
187 /// initialization). | |
188 void dump(Ostream &Stream) const; | |
189 | |
190 private: | |
191 // list of initializers associated with the global address. | |
192 InitializerListType Initializers; | |
193 // The name for the global. | |
194 IceString Name; | |
195 // The alignment of the initializer. | |
196 uint32_t Alignment; | |
197 // True if a constant initializer. | |
198 bool IsConstant; | |
199 // True if the address is internal. | |
200 bool IsInternal; | |
201 }; | |
202 | |
203 template <class StreamType> | |
204 inline StreamType &operator<<(StreamType &Stream, | |
205 const GlobalAddress::Initializer &Init) { | |
206 Init.dump(Stream); | |
207 return Stream; | |
208 } | |
209 | |
210 template <class StreamType> | |
211 inline StreamType &operator<<(StreamType &Stream, const GlobalAddress &Addr) { | |
212 Addr.dump(Stream); | |
213 return Stream; | |
214 } | |
215 | |
216 } // end of namespace Ice | |
217 | |
218 #endif // SUBZERO_SRC_ICEGLOBALINITS_H | |
OLD | NEW |