Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: src/IceGlobalInits.h

Issue 624663002: Introduce model of global initializers in Subzero. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix nits. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceGlobalInits.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 and
11 // initializers in Subzero. Global initializers are represented as a
12 // 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 GlobalAddress(const GlobalAddress &) = delete;
31 GlobalAddress &operator=(const GlobalAddress &) = delete;
32
33 public:
34 /// Base class for a global variable initializer.
35 class Initializer {
36 Initializer(const Initializer &) = delete;
37 Initializer &operator=(const Initializer &) = delete;
38
39 public:
40 /// Discriminator for LLVM-style RTTI.
41 enum InitializerKind {
42 DataInitializerKind,
43 ZeroInitializerKind,
44 RelocInitializerKind
45 };
46 InitializerKind getKind() const { return Kind; }
47 virtual ~Initializer() {}
48 virtual SizeT getNumBytes() const = 0;
49 virtual void dump(Ostream &Stream) const = 0;
50 virtual void dumpType(Ostream &Stream) const;
51
52 protected:
53 explicit Initializer(InitializerKind Kind) : Kind(Kind) {}
54
55 private:
56 const InitializerKind Kind;
57 };
58
59 // Models the data in a data initializer.
60 typedef std::vector<uint8_t> DataVecType;
61
62 /// Defines a sequence of byte values as a data initializer.
63 class DataInitializer : public Initializer {
64 DataInitializer(const DataInitializer &) = delete;
65 DataInitializer &operator=(const DataInitializer &) = delete;
66
67 public:
68 template <class IntContainer>
69 DataInitializer(const IntContainer &Values)
70 : Initializer(DataInitializerKind), Contents(Values.size()) {
71 size_t i = 0;
72 for (auto &V : Values) {
73 Contents[i] = static_cast<uint8_t>(V);
74 ++i;
75 }
76 }
77 DataInitializer(const char *Str, size_t StrLen)
78 : Initializer(DataInitializerKind), Contents(StrLen) {
79 for (size_t i = 0; i < StrLen; ++i)
80 Contents[i] = static_cast<uint8_t>(Str[i]);
81 }
82 ~DataInitializer() override {}
83 const DataVecType &getContents() const { return Contents; }
84 SizeT getNumBytes() const override { return Contents.size(); }
85 void dump(Ostream &Stream) const override;
86 static bool classof(const Initializer *D) {
87 return D->getKind() == DataInitializerKind;
88 }
89
90 private:
91 // The byte contents of the data initializer.
92 DataVecType Contents;
93 };
94
95 /// Defines a sequence of bytes initialized to zero.
96 class ZeroInitializer : public Initializer {
97 ZeroInitializer(const ZeroInitializer &) = delete;
98 ZeroInitializer &operator=(const ZeroInitializer &) = delete;
99
100 public:
101 explicit ZeroInitializer(SizeT Size)
102 : Initializer(ZeroInitializerKind), Size(Size) {}
103 ~ZeroInitializer() override {}
104 SizeT getNumBytes() const override { return Size; }
105 void dump(Ostream &Stream) const override;
106 static bool classof(const Initializer *Z) {
107 return Z->getKind() == ZeroInitializerKind;
108 }
109
110 private:
111 // The number of bytes to be zero initialized.
112 SizeT Size;
113 };
114
115 /// Defines the kind of relocation addresses allowed.
116 enum RelocationKind { FunctionRelocation, GlobalAddressRelocation };
117
118 /// Defines a relocation address (i.e. reference to a function
119 /// or global variable address).
120 class RelocationAddress {
121 RelocationAddress &operator=(const RelocationAddress &) = delete;
122
123 public:
124 explicit RelocationAddress(const RelocationAddress &Addr)
125 : Kind(Addr.Kind) {
126 switch (Kind) {
127 case FunctionRelocation:
128 Address.Function = Addr.Address.Function;
129 break;
130 case GlobalAddressRelocation:
131 Address.GlobalAddr = Addr.Address.GlobalAddr;
132 }
133 }
134 explicit RelocationAddress(llvm::Value *Function)
135 : Kind(FunctionRelocation) {
136 Address.Function = Function;
137 }
138 explicit RelocationAddress(GlobalAddress *GlobalAddr)
139 : Kind(GlobalAddressRelocation) {
140 Address.GlobalAddr = GlobalAddr;
141 }
142 RelocationKind getKind() const { return Kind; }
143 llvm::Value *getFunction() const {
144 assert(Kind == FunctionRelocation);
145 return Address.Function;
146 }
147 GlobalAddress *getGlobalAddr() const {
148 assert(Kind == GlobalAddressRelocation);
149 return Address.GlobalAddr;
150 }
151 private:
152 const RelocationKind Kind;
153 union {
154 // TODO(kschimpf) Integrate Functions into ICE model.
155 llvm::Value *Function;
156 GlobalAddress *GlobalAddr;
157 } Address;
158 };
159
160 // Relocation address offsets must be 32 bit values.
161 typedef int32_t RelocOffsetType;
162 static const SizeT RelocAddrSize = 4;
163
164 /// Defines the relocation value of another address.
165 class RelocInitializer : public Initializer {
166 RelocInitializer(const RelocInitializer &) = delete;
167 RelocInitializer &operator=(const RelocInitializer &) = delete;
168
169 public:
170 RelocInitializer(const RelocationAddress &Address, RelocOffsetType Offset)
171 : Initializer(RelocInitializerKind), Address(Address), Offset(Offset) {}
172 ~RelocInitializer() override {}
173 RelocOffsetType getOffset() const { return Offset; }
174 IceString getName() const;
175 SizeT getNumBytes() const override { return RelocAddrSize; }
176 void dump(Ostream &Stream) const override;
177 virtual void dumpType(Ostream &Stream) const;
178 static bool classof(const Initializer *R) {
179 return R->getKind() == RelocInitializerKind;
180 }
181
182 private:
183 // The global address used in the relocation.
184 const RelocationAddress Address;
185 // The offset to add to the relocation.
186 const RelocOffsetType Offset;
187 };
188
189 /// Models the list of initializers.
190 typedef std::vector<Initializer *> InitializerListType;
191
192 GlobalAddress() : Alignment(0), IsConstant(false), IsInternal(true) {}
193 ~GlobalAddress();
194
195 const InitializerListType &getInitializers() const { return Initializers; }
196 bool hasName() const { return !Name.empty(); }
197 const IceString &getName() const { return Name; }
198 void setName(const IceString &NewName) { Name = NewName; }
199 bool getIsConstant() const { return IsConstant; }
200 void setIsConstant(bool NewValue) { IsConstant = NewValue; }
201 uint32_t getAlignment() const { return Alignment; }
202 void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; }
203 bool getIsInternal() const { return IsInternal; }
204 void setIsInternal(bool NewValue) { IsInternal = NewValue; }
205
206 /// Returns the number of bytes for the initializer of the global
207 /// address.
208 SizeT getNumBytes() const {
209 SizeT Count = 0;
210 for (Initializer *Init : Initializers) {
211 Count += Init->getNumBytes();
212 }
213 return Count;
214 }
215
216 /// Adds Initializer to the list of initializers. Takes ownership of
217 /// the initializer.
218 void addInitializer(Initializer *Initializer) {
219 Initializers.push_back(Initializer);
220 }
221
222 /// Prints out type for initializer associated with the global address
223 /// to Stream.
224 void dumpType(Ostream &Stream) const;
225
226 /// Prints out the definition of the global address (including
227 /// initialization).
228 void dump(Ostream &Stream) const;
229
230 private:
231 // list of initializers associated with the global address.
232 InitializerListType Initializers;
233 // The name for the global.
234 IceString Name;
235 // The alignment of the initializer.
236 uint32_t Alignment;
237 // True if a constant initializer.
238 bool IsConstant;
239 // True if the address is internal.
240 bool IsInternal;
241 };
242
243 template <class StreamType>
244 inline StreamType &operator<<(StreamType &Stream,
245 const GlobalAddress::Initializer &Init) {
246 Init.dump(Stream);
247 return Stream;
248 }
249
250 template <class StreamType>
251 inline StreamType &operator<<(StreamType &Stream, const GlobalAddress &Addr) {
252 Addr.dump(Stream);
253 return Stream;
254 }
255
256 } // end of namespace Ice
257
258 #endif // SUBZERO_SRC_ICEGLOBALINITS_H
OLDNEW
« no previous file with comments | « src/IceConverter.cpp ('k') | src/IceGlobalInits.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698