1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
//===----- ConstantBuilder.h - Builder for LLVM IR constants ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class provides a convenient interface for building complex
// global initializers.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_LIB_CODEGEN_CONSTANTBUILDER_H
#define LLVM_CLANG_LIB_CODEGEN_CONSTANTBUILDER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Constants.h"
#include "CodeGenModule.h"
namespace clang {
namespace CodeGen {
class ConstantStructBuilder;
class ConstantArrayBuilder;
/// A convenience builder class for complex constant initializers,
/// especially for anonymous global structures used by various language
/// runtimes.
///
/// The basic usage pattern is expected to be something like:
/// ConstantInitBuilder builder(CGM);
/// auto toplevel = builder.beginStruct();
/// toplevel.addInt(CGM.SizeTy, widgets.size());
/// auto widgetArray = builder.beginArray();
/// for (auto &widget : widgets) {
/// auto widgetDesc = widgetArray.beginStruct();
/// widgetDesc.addInt(CGM.SizeTy, widget.getPower());
/// widgetDesc.add(CGM.GetAddrOfConstantString(widget.getName()));
/// widgetDesc.add(CGM.GetAddrOfGlobal(widget.getInitializerDecl()));
/// widgetArray.add(widgetDesc.finish());
/// }
/// toplevel.add(widgetArray.finish());
/// auto global = toplevel.finishAndCreateGlobal("WIDGET_LIST", Align,
/// /*constant*/ true);
class ConstantInitBuilder {
CodeGenModule &CGM;
llvm::SmallVector<llvm::Constant*, 16> Buffer;
bool Frozen = false;
public:
explicit ConstantInitBuilder(CodeGenModule &CGM) : CGM(CGM) {}
~ConstantInitBuilder() {
assert(Buffer.empty() && "didn't claim all values out of buffer");
}
class AggregateBuilderBase {
protected:
ConstantInitBuilder &Builder;
AggregateBuilderBase *Parent;
size_t Begin;
bool Finished = false;
bool Frozen = false;
llvm::SmallVectorImpl<llvm::Constant*> &getBuffer() {
return Builder.Buffer;
}
const llvm::SmallVectorImpl<llvm::Constant*> &getBuffer() const {
return Builder.Buffer;
}
AggregateBuilderBase(ConstantInitBuilder &builder,
AggregateBuilderBase *parent)
: Builder(builder), Parent(parent), Begin(builder.Buffer.size()) {
if (parent) {
assert(!parent->Frozen && "parent already has child builder active");
parent->Frozen = true;
} else {
assert(!builder.Frozen && "builder already has child builder active");
builder.Frozen = true;
}
}
~AggregateBuilderBase() {
assert(Finished && "didn't claim value from aggregate builder");
}
void markFinished() {
assert(!Frozen && "child builder still active");
assert(!Finished && "builder already finished");
Finished = true;
if (Parent) {
assert(Parent->Frozen &&
"parent not frozen while child builder active");
Parent->Frozen = false;
} else {
assert(Builder.Frozen &&
"builder not frozen while child builder active");
Builder.Frozen = false;
}
}
public:
// Not copyable.
AggregateBuilderBase(const AggregateBuilderBase &) = delete;
AggregateBuilderBase &operator=(const AggregateBuilderBase &) = delete;
// Movable, mostly to allow returning. But we have to write this out
// properly to satisfy the assert in the destructor.
AggregateBuilderBase(AggregateBuilderBase &&other)
: Builder(other.Builder), Parent(other.Parent), Begin(other.Begin),
Finished(other.Finished), Frozen(other.Frozen) {
other.Finished = false;
}
AggregateBuilderBase &operator=(AggregateBuilderBase &&other) = delete;
void add(llvm::Constant *value) {
assert(!Finished && "cannot add more values after finishing builder");
Builder.Buffer.push_back(value);
}
void addSize(CharUnits size) {
add(Builder.CGM.getSize(size));
}
void addInt(llvm::IntegerType *intTy, uint64_t value,
bool isSigned = false) {
add(llvm::ConstantInt::get(intTy, value, isSigned));
}
void addNullPointer(llvm::PointerType *ptrTy) {
add(llvm::ConstantPointerNull::get(ptrTy));
}
ArrayRef<llvm::Constant*> getGEPIndicesToCurrentPosition(
llvm::SmallVectorImpl<llvm::Constant*> &indices) {
getGEPIndicesTo(indices, Builder.Buffer.size());
return indices;
}
ConstantArrayBuilder beginArray(llvm::Type *eltTy = nullptr);
ConstantStructBuilder beginStruct(llvm::StructType *structTy = nullptr);
private:
void getGEPIndicesTo(llvm::SmallVectorImpl<llvm::Constant*> &indices,
size_t position) const {
// Recurse on the parent builder if present.
if (Parent) {
Parent->getGEPIndicesTo(indices, Begin);
// Otherwise, add an index to drill into the first level of pointer.
} else {
assert(indices.empty());
indices.push_back(llvm::ConstantInt::get(Builder.CGM.SizeTy, 0));
}
assert(position >= Begin);
indices.push_back(llvm::ConstantInt::get(Builder.CGM.SizeTy,
position - Begin));
}
};
template <class Impl>
class AggregateBuilder : public AggregateBuilderBase {
protected:
AggregateBuilder(ConstantInitBuilder &builder,
AggregateBuilderBase *parent)
: AggregateBuilderBase(builder, parent) {}
Impl &asImpl() { return *static_cast<Impl*>(this); }
public:
/// Given that this builder was created by beginning an array or struct
/// component on the given parent builder, finish the array/struct
/// component and add it to the parent.
///
/// It is an intentional choice that the parent is passed in explicitly
/// despite it being redundant with information already kept in the
/// builder. This aids in readability by making it easier to find the
/// places that add components to a builder, as well as "bookending"
/// the sub-builder more explicitly.
void finishAndAddTo(AggregateBuilderBase &parent) {
assert(Parent == &parent && "adding to non-parent builder");
parent.add(asImpl().finishImpl());
}
/// Given that this builder was created by beginning an array or struct
/// directly on a ConstantInitBuilder, finish the array/struct and
/// create a global variable with it as the initializer.
template <class... As>
llvm::GlobalVariable *finishAndCreateGlobal(As &&...args) {
assert(!Parent && "finishing non-root builder");
return Builder.createGlobal(asImpl().finishImpl(),
std::forward<As>(args)...);
}
/// Given that this builder was created by beginning an array or struct
/// directly on a ConstantInitBuilder, finish the array/struct and
/// set it as the initializer of the given global variable.
void finishAndSetAsInitializer(llvm::GlobalVariable *global) {
assert(!Parent && "finishing non-root builder");
return Builder.setGlobalInitializer(global, asImpl().finishImpl());
}
};
ConstantArrayBuilder beginArray(llvm::Type *eltTy = nullptr);
ConstantStructBuilder beginStruct(llvm::StructType *structTy = nullptr);
private:
llvm::GlobalVariable *createGlobal(llvm::Constant *initializer,
StringRef name,
CharUnits alignment,
bool constant = false,
llvm::GlobalValue::LinkageTypes linkage
= llvm::GlobalValue::InternalLinkage,
unsigned addressSpace = 0) {
auto GV = new llvm::GlobalVariable(CGM.getModule(),
initializer->getType(),
constant,
linkage,
initializer,
name,
/*insert before*/ nullptr,
llvm::GlobalValue::NotThreadLocal,
addressSpace);
GV->setAlignment(alignment.getQuantity());
return GV;
}
void setGlobalInitializer(llvm::GlobalVariable *GV,
llvm::Constant *initializer) {
GV->setInitializer(initializer);
}
};
/// A helper class of ConstantInitBuilder, used for building constant
/// array initializers.
class ConstantArrayBuilder
: public ConstantInitBuilder::AggregateBuilder<ConstantArrayBuilder> {
llvm::Type *EltTy;
friend class ConstantInitBuilder;
template <class Impl> friend class ConstantInitBuilder::AggregateBuilder;
ConstantArrayBuilder(ConstantInitBuilder &builder,
AggregateBuilderBase *parent, llvm::Type *eltTy)
: AggregateBuilder(builder, parent), EltTy(eltTy) {}
public:
size_t size() const {
assert(!Finished);
assert(!Frozen);
assert(Begin <= getBuffer().size());
return getBuffer().size() - Begin;
}
private:
/// Form an array constant from the values that have been added to this
/// builder.
llvm::Constant *finishImpl() {
markFinished();
auto &buffer = getBuffer();
assert((Begin < buffer.size() ||
(Begin == buffer.size() && EltTy))
&& "didn't add any array elements without element type");
auto elts = llvm::makeArrayRef(buffer).slice(Begin);
auto eltTy = EltTy ? EltTy : elts[0]->getType();
auto type = llvm::ArrayType::get(eltTy, elts.size());
auto constant = llvm::ConstantArray::get(type, elts);
buffer.erase(buffer.begin() + Begin, buffer.end());
return constant;
}
};
inline ConstantArrayBuilder
ConstantInitBuilder::beginArray(llvm::Type *eltTy) {
return ConstantArrayBuilder(*this, nullptr, eltTy);
}
inline ConstantArrayBuilder
ConstantInitBuilder::AggregateBuilderBase::beginArray(llvm::Type *eltTy) {
return ConstantArrayBuilder(Builder, this, eltTy);
}
/// A helper class of ConstantInitBuilder, used for building constant
/// struct initializers.
class ConstantStructBuilder
: public ConstantInitBuilder::AggregateBuilder<ConstantStructBuilder> {
llvm::StructType *Ty;
friend class ConstantInitBuilder;
template <class Impl> friend class ConstantInitBuilder::AggregateBuilder;
ConstantStructBuilder(ConstantInitBuilder &builder,
AggregateBuilderBase *parent, llvm::StructType *ty)
: AggregateBuilder(builder, parent), Ty(ty) {}
/// Finish the struct.
llvm::Constant *finishImpl() {
markFinished();
auto &buffer = getBuffer();
assert(Begin < buffer.size() && "didn't add any struct elements?");
auto elts = llvm::makeArrayRef(buffer).slice(Begin);
llvm::Constant *constant;
if (Ty) {
constant = llvm::ConstantStruct::get(Ty, elts);
} else {
constant = llvm::ConstantStruct::getAnon(elts, /*packed*/ false);
}
buffer.erase(buffer.begin() + Begin, buffer.end());
return constant;
}
};
inline ConstantStructBuilder
ConstantInitBuilder::beginStruct(llvm::StructType *structTy) {
return ConstantStructBuilder(*this, nullptr, structTy);
}
inline ConstantStructBuilder
ConstantInitBuilder::AggregateBuilderBase::beginStruct(
llvm::StructType *structTy) {
return ConstantStructBuilder(Builder, this, structTy);
}
} // end namespace CodeGen
} // end namespace clang
#endif
|