forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiraggr.cpp
278 lines (231 loc) · 9.12 KB
/
iraggr.cpp
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
//===-- iraggr.cpp --------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/llvm.h"
#include "aggregate.h"
#include "declaration.h"
#include "init.h"
#include "mtype.h"
#include "target.h"
#include "gen/irstate.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/tollvm.h"
#include "ir/iraggr.h"
#include "irdsymbol.h"
#include "ir/irtypeclass.h"
#include "ir/irtypestruct.h"
#include <algorithm>
//////////////////////////////////////////////////////////////////////////////
llvm::StructType *IrAggr::getLLStructType() {
if (llStructType)
return llStructType;
LLType *llType = DtoType(type);
if (auto irClassType = type->ctype->isClass())
llType = irClassType->getMemoryLLType();
llStructType = llvm::dyn_cast<LLStructType>(llType);
return llStructType;
}
//////////////////////////////////////////////////////////////////////////////
LLGlobalVariable *IrAggr::getInitSymbol() {
if (init) {
return init;
}
// create the initZ symbol
auto initname = getMangledInitSymbolName(aggrdecl);
init =
getOrCreateGlobal(aggrdecl->loc, gIR->module, getLLStructType(), true,
llvm::GlobalValue::ExternalLinkage, nullptr, initname);
// set alignment
init->setAlignment(DtoAlignment(type));
return init;
}
//////////////////////////////////////////////////////////////////////////////
llvm::Constant *IrAggr::getDefaultInit() {
if (constInit) {
return constInit;
}
IF_LOG Logger::println("Building default initializer for %s",
aggrdecl->toPrettyChars());
LOG_SCOPE;
VarInitMap noExplicitInitializers;
constInit = createInitializerConstant(noExplicitInitializers);
return constInit;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// helper function that adds zero bytes to a vector of constants
// FIXME A similar function is in ir/irtypeaggr.cpp
static inline size_t
add_zeros(llvm::SmallVectorImpl<llvm::Constant *> &constants,
size_t startOffset, size_t endOffset) {
assert(startOffset <= endOffset);
const size_t paddingSize = endOffset - startOffset;
if (paddingSize) {
llvm::ArrayType *pad = llvm::ArrayType::get(
llvm::Type::getInt8Ty(gIR->context()), paddingSize);
constants.push_back(llvm::Constant::getNullValue(pad));
}
return paddingSize ? 1 : 0;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
LLConstant *IrAggr::getDefaultInitializer(VarDeclaration *field) {
if (field->_init) {
// Issue 9057 workaround caused by issue 14666 fix, see DMD upstream
// commit 069f570005.
if (field->sem < Semantic2Done && field->_scope) {
field->semantic2(field->_scope);
}
return DtoConstInitializer(field->_init->loc, field->type, field->_init);
}
return DtoConstExpInit(field->loc, field->type,
field->type->defaultInit(field->loc));
}
// return a constant array of type arrTypeD initialized with a constant value,
// or that constant value
static llvm::Constant *FillSArrayDims(Type *arrTypeD, llvm::Constant *init) {
// Check whether we actually need to expand anything.
// KLUDGE: We don't have the initializer type here, so we can only check
// the size without doing an expensive recursive D <-> LLVM type comparison.
// The better way to solve this would be to just fix the initializer
// codegen in any place where a scalar initializer might still be generated.
if (gDataLayout->getTypeStoreSize(init->getType()) >= arrTypeD->size()) {
return init;
}
if (arrTypeD->ty == Tsarray) {
init = FillSArrayDims(arrTypeD->nextOf(), init);
size_t dim = static_cast<TypeSArray *>(arrTypeD)->dim->toUInteger();
llvm::ArrayType *arrty = llvm::ArrayType::get(init->getType(), dim);
return llvm::ConstantArray::get(arrty,
std::vector<llvm::Constant *>(dim, init));
}
return init;
}
llvm::Constant *
IrAggr::createInitializerConstant(const VarInitMap &explicitInitializers) {
IF_LOG Logger::println("Creating initializer constant for %s",
aggrdecl->toChars());
LOG_SCOPE;
llvm::SmallVector<llvm::Constant *, 16> constants;
unsigned offset = 0;
if (type->ty == Tclass) {
// add vtbl
constants.push_back(getVtblSymbol());
offset += Target::ptrsize;
// add monitor (except for C++ classes)
if (!aggrdecl->isClassDeclaration()->isCPPclass()) {
constants.push_back(getNullValue(getVoidPtrType()));
offset += Target::ptrsize;
}
}
// Add the initializers for the member fields. While we are traversing the
// class hierarchy, use the opportunity to populate interfacesWithVtbls if
// we haven't done so previously (due to e.g. ClassReferenceExp, we can
// have multiple initializer constants for a single class).
addFieldInitializers(constants, explicitInitializers, aggrdecl, offset,
interfacesWithVtbls.empty());
// tail padding?
const size_t structsize = aggrdecl->size(Loc());
if (offset < structsize)
add_zeros(constants, offset, structsize);
assert(!constants.empty());
// get LL field types
llvm::SmallVector<llvm::Type *, 16> types;
types.reserve(constants.size());
for (auto c : constants)
types.push_back(c->getType());
auto llStructType = getLLStructType();
bool isCompatible = (types.size() == llStructType->getNumElements());
if (isCompatible) {
for (size_t i = 0; i < types.size(); i++) {
if (types[i] != llStructType->getElementType(i)) {
isCompatible = false;
break;
}
}
}
// build constant
LLStructType *llType =
isCompatible ? llStructType
: LLStructType::get(gIR->context(), types, isPacked());
llvm::Constant *c = LLConstantStruct::get(llType, constants);
IF_LOG Logger::cout() << "final initializer: " << *c << std::endl;
return c;
}
void IrAggr::addFieldInitializers(
llvm::SmallVectorImpl<llvm::Constant *> &constants,
const VarInitMap &explicitInitializers, AggregateDeclaration *decl,
unsigned &offset, bool populateInterfacesWithVtbls) {
if (ClassDeclaration *cd = decl->isClassDeclaration()) {
if (cd->baseClass) {
addFieldInitializers(constants, explicitInitializers, cd->baseClass,
offset, populateInterfacesWithVtbls);
}
// has interface vtbls?
if (cd->vtblInterfaces && cd->vtblInterfaces->dim > 0) {
// Align interface infos to pointer size.
unsigned aligned =
(offset + Target::ptrsize - 1) & ~(Target::ptrsize - 1);
if (offset < aligned) {
add_zeros(constants, offset, aligned);
offset = aligned;
}
// false when it's not okay to use functions from super classes
bool newinsts = (cd == aggrdecl->isClassDeclaration());
size_t inter_idx = interfacesWithVtbls.size();
for (auto bc : *cd->vtblInterfaces) {
constants.push_back(getInterfaceVtbl(bc, newinsts, inter_idx));
offset += Target::ptrsize;
inter_idx++;
if (populateInterfacesWithVtbls)
interfacesWithVtbls.push_back(bc);
}
}
}
AggrTypeBuilder b(false, offset);
b.addAggregate(decl, &explicitInitializers, AggrTypeBuilder::Aliases::Skip);
offset = b.currentOffset();
const size_t baseLLFieldIndex = constants.size();
const size_t numNewLLFields = b.defaultTypes().size();
constants.resize(constants.size() + numNewLLFields, nullptr);
// add explicit and non-overlapping implicit initializers
for (const auto &pair : b.varGEPIndices()) {
const auto field = pair.first;
const size_t fieldIndex = pair.second;
const auto explicitIt = explicitInitializers.find(field);
llvm::Constant *init = (explicitIt != explicitInitializers.end()
? explicitIt->second
: getDefaultInitializer(field));
constants[baseLLFieldIndex + fieldIndex] =
FillSArrayDims(field->type, init);
}
// zero out remaining padding fields
for (size_t i = 0; i < numNewLLFields; i++) {
auto &init = constants[baseLLFieldIndex + i];
if (!init)
init = llvm::Constant::getNullValue(b.defaultTypes()[i]);
}
}
IrAggr *getIrAggr(AggregateDeclaration *decl, bool create) {
if (!isIrAggrCreated(decl) && create) {
assert(decl->ir->irAggr == NULL);
decl->ir->irAggr = new IrAggr(decl);
decl->ir->m_type = IrDsymbol::AggrType;
}
assert(decl->ir->irAggr != NULL);
return decl->ir->irAggr;
}
bool isIrAggrCreated(AggregateDeclaration *decl) {
int t = decl->ir->type();
assert(t == IrDsymbol::AggrType || t == IrDsymbol::NotSet);
return t == IrDsymbol::AggrType;
}