forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclasses.cpp
More file actions
507 lines (415 loc) · 15.4 KB
/
classes.cpp
File metadata and controls
507 lines (415 loc) · 15.4 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//===-- classes.cpp -------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/classes.h"
#include "dmd/aggregate.h"
#include "dmd/declaration.h"
#include "dmd/errors.h"
#include "dmd/expression.h"
#include "dmd/identifier.h"
#include "dmd/init.h"
#include "dmd/mtype.h"
#include "dmd/target.h"
#include "gen/arrays.h"
#include "gen/dvalue.h"
#include "gen/functions.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/nested.h"
#include "gen/optimizer.h"
#include "gen/runtime.h"
#include "gen/structs.h"
#include "gen/tollvm.h"
#include "ir/iraggr.h"
#include "ir/irdsymbol.h"
#include "ir/irfunction.h"
#include "ir/irtypeclass.h"
////////////////////////////////////////////////////////////////////////////////
// FIXME: this needs to be cleaned up
void DtoResolveClass(ClassDeclaration *cd) {
if (cd->ir->isResolved()) {
return;
}
cd->ir->setResolved();
IF_LOG Logger::println("DtoResolveClass(%s): %s", cd->toPrettyChars(),
cd->loc.toChars());
LOG_SCOPE;
// make sure the base classes are processed first
for (auto bc : *cd->baseclasses) {
DtoResolveClass(bc->sym);
}
// make sure type exists
DtoType(cd->type);
// create IrAggr
getIrAggr(cd, true);
// make sure all fields really get their ir field
for (auto vd : cd->fields) {
IF_LOG {
if (isIrFieldCreated(vd)) {
Logger::println("class field already exists");
}
}
getIrField(vd, true);
}
}
////////////////////////////////////////////////////////////////////////////////
DValue *DtoNewClass(Loc loc, TypeClass *tc, NewExp *newexp) {
// resolve type
DtoResolveClass(tc->sym);
const auto irClass = getIrAggr(tc->sym);
// allocate
LLValue *mem;
bool doInit = true;
if (newexp->placement) {
mem = DtoLVal(newexp->placement);
} else if (newexp->onstack) {
mem = DtoRawAlloca(irClass->getLLStructType(), tc->sym->alignsize,
".newclass_alloca");
} else if (global.params.ehnogc && newexp->thrownew) {
// _d_newThrowable template lowering
assert(newexp->lowering);
mem = DtoRVal(newexp->lowering);
doInit = false;
} else {
llvm::Function *fn = getRuntimeFunction(loc, gIR->module, "_d_allocclass");
LLConstant *ci = irClass->getClassInfoSymbol();
mem = gIR->CreateCallOrInvoke(fn, ci, ".newclass_gc");
}
// init
if (doInit)
DtoInitClass(tc, mem);
// init inner-class outer reference
if (newexp->thisexp) {
Logger::println("Resolving outer class");
LOG_SCOPE;
unsigned idx = getFieldGEPIndex(tc->sym, tc->sym->vthis);
LLValue *src = DtoRVal(newexp->thisexp);
LLValue *dst = DtoGEP(irClass->getLLStructType(), mem, 0, idx);
IF_LOG Logger::cout() << "dst: " << *dst << "\nsrc: " << *src << '\n';
DtoStore(src, dst);
}
// set the context for nested classes
else if (tc->sym->isNested() && tc->sym->vthis) {
DtoResolveNestedContext(loc, tc->sym, mem);
}
// call constructor
if (newexp->member) {
// evaluate argprefix
if (newexp->argprefix) {
toElemDtor(newexp->argprefix);
}
Logger::println("Calling constructor");
assert(newexp->arguments != NULL);
DFuncValue dfn(newexp->member, DtoCallee(newexp->member), mem);
// ignore ctor return value (C++ ctors on Posix may not return `this`)
DtoCallFunction(newexp->loc, tc, &dfn, newexp->arguments);
return new DImValue(tc, mem);
}
assert(newexp->argprefix == NULL);
// return default constructed class
return new DImValue(tc, mem);
}
////////////////////////////////////////////////////////////////////////////////
void DtoInitClass(TypeClass *tc, LLValue *dst) {
DtoResolveClass(tc->sym);
IrClass *irClass = getIrAggr(tc->sym);
llvm::StructType *st = irClass->getLLStructType();
// Set vtable field. Doing this seperately might be optimized better.
LLValue *tmp = DtoGEP(st, dst, 0u, 0, "vtbl");
LLValue *val = irClass->getVtblSymbol();
DtoStore(val, tmp);
// For D classes, set the monitor field to null.
const bool isCPPclass = tc->sym->isCPPclass() ? true : false;
if (!isCPPclass) {
tmp = DtoGEP(st, dst, 0, 1, "monitor");
val = LLConstant::getNullValue(st->getElementType(1));
DtoStore(val, tmp);
}
// Copy the rest from the static initializer, if any.
unsigned const firstDataIdx = isCPPclass ? 1 : 2;
uint64_t const dataBytes =
tc->sym->structsize - target.ptrsize * firstDataIdx;
if (dataBytes == 0) {
return;
}
LLValue *dstarr = DtoGEP(st, dst, 0, firstDataIdx);
// init symbols might not have valid types
LLValue *initsym = irClass->getInitSymbol();
LLValue *srcarr = DtoGEP(st, initsym, 0, firstDataIdx);
DtoMemCpy(dstarr, srcarr, DtoConstSize_t(dataBytes));
}
////////////////////////////////////////////////////////////////////////////////
void DtoFinalizeClass(Loc loc, LLValue *inst) {
// get runtime function
llvm::Function *fn =
getRuntimeFunction(loc, gIR->module, "_d_callfinalizer");
gIR->CreateCallOrInvoke(fn, inst, "");
}
////////////////////////////////////////////////////////////////////////////////
void DtoFinalizeScopeClass(Loc loc, DValue *dval,
bool dynTypeMatchesStaticType) {
llvm::Value *inst = DtoRVal(dval);
if (!isOptimizationEnabled() || !dynTypeMatchesStaticType) {
DtoFinalizeClass(loc, inst);
return;
}
bool hasDtor = false;
const auto cd = dval->type->toBasetype()->isTypeClass()->sym;
for (auto cd2 = cd; cd2; cd2 = cd2->baseClass) {
if (cd2->dtor) {
hasDtor = true;
break;
}
}
if (hasDtor) {
DtoFinalizeClass(loc, inst);
return;
}
// no dtors => only finalize (via druntime call) if monitor is set,
// see https://github.com/ldc-developers/ldc/issues/2515
llvm::BasicBlock *ifbb = gIR->insertBB("if");
llvm::BasicBlock *endbb = gIR->insertBBAfter(ifbb, "endif");
llvm::StructType *st =
isaStruct(getIrType(cd->type, true)->isClass()->getMemoryLLType());
const auto monitor =
DtoLoad(st->getElementType(1), DtoGEP(st, inst, 0, 1), ".monitor");
const auto hasMonitor =
gIR->ir->CreateICmp(llvm::CmpInst::ICMP_NE, monitor,
getNullValue(monitor->getType()), ".hasMonitor");
llvm::BranchInst::Create(ifbb, endbb, hasMonitor, gIR->scopebb());
gIR->ir->SetInsertPoint(ifbb);
DtoFinalizeClass(loc, inst);
gIR->ir->CreateBr(endbb);
gIR->ir->SetInsertPoint(endbb);
}
////////////////////////////////////////////////////////////////////////////////
DValue *DtoCastClass(Loc loc, DValue *val, Type *_to) {
IF_LOG Logger::println("DtoCastClass(%s, %s)", val->type->toChars(),
_to->toChars());
LOG_SCOPE;
Type *to = _to->toBasetype();
// class -> pointer
if (to->ty == TY::Tpointer) {
IF_LOG Logger::println("to pointer");
return new DImValue(_to, DtoRVal(val));
}
// class -> bool
if (to->ty == TY::Tbool) {
IF_LOG Logger::println("to bool");
LLValue *llval = DtoRVal(val);
LLValue *zero = LLConstant::getNullValue(llval->getType());
return new DImValue(_to, gIR->ir->CreateICmpNE(llval, zero));
}
// class -> integer
if (to->isIntegral()) {
IF_LOG Logger::println("to %s", to->toChars());
// get class ptr
LLValue *v = DtoRVal(val);
// cast to size_t
v = gIR->ir->CreatePtrToInt(v, DtoSize_t(), "");
// cast to the final int type
DImValue im(Type::tsize_t, v);
return DtoCastInt(loc, &im, _to);
}
// class -> typeof(null)
if (to->ty == TY::Tnull) {
IF_LOG Logger::println("to %s", to->toChars());
return new DImValue(_to, LLConstant::getNullValue(DtoType(_to)));
}
// must be class/interface
assert(to->ty == TY::Tclass);
TypeClass *tc = static_cast<TypeClass *>(to);
// from type
Type *from = val->type->toBasetype();
TypeClass *fc = static_cast<TypeClass *>(from);
// copy DMD logic:
// if to isBaseOf from with offset: (to ? to + offset : null)
// else if from is C++ and to is C++: to
// else if from is C++ and to is D: null
// else if from is interface: _d_interface_cast(to)
// else if from is class: _d_dynamic_cast(to)
int offset = 0;
if (tc->sym->isBaseOf(fc->sym, &offset)) {
Logger::println("static down cast");
// interface types don't cover the full object in case of multiple inheritence
// so GEP on the original type is inappropriate
// offset pointer
LLValue *orig = DtoRVal(val);
LLValue *v = orig;
if (offset != 0) {
assert(offset > 0);
v = DtoGEP1(getI8Type(), v, DtoConstUint(offset));
}
IF_LOG {
Logger::cout() << "V = " << *v << std::endl;
}
// Check whether the original value was null, and return null if so.
// Sure we could have jumped over the code above in this case, but
// it's just a GEP and (maybe) a pointer-to-pointer BitCast, so it
// should be pretty cheap and perfectly safe even if the original was
// null.
const auto nullPtr = getNullPtr();
LLValue *isNull = gIR->ir->CreateICmpEQ(orig, nullPtr, ".nullcheck");
v = gIR->ir->CreateSelect(isNull, nullPtr, v, ".interface");
// return r-value
return new DImValue(_to, v);
}
if (fc->sym->classKind == ClassKind::cpp) {
Logger::println("C++ class/interface cast");
LLValue *v =
tc->sym->classKind == ClassKind::cpp ? DtoRVal(val) : getNullPtr();
return new DImValue(_to, v);
}
// from interface
if (fc->sym->isInterfaceDeclaration()) {
Logger::println("interface cast");
return DtoDynamicCastInterface(loc, val, _to);
}
// from class
Logger::println("dynamic up cast");
return DtoDynamicCastObject(loc, val, _to);
}
bool DtoIsObjcLinkage(Type *_to) {
if (auto to = _to->isTypeClass()) {
DtoResolveClass(to->sym);
return to->sym->classKind == ClassKind::objc;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
static void resolveObjectAndClassInfoClasses() {
// check declarations in object.d
getObjectType();
getClassInfoType();
DtoResolveClass(ClassDeclaration::object);
DtoResolveClass(Type::typeinfoclass);
}
DValue *DtoDynamicCastObject(Loc loc, DValue *val, Type *_to) {
resolveObjectAndClassInfoClasses();
// Dynamic casting in Objective-C works differently from D.
// We call objc_opt_isKindOfClass to get a bool defining
// whether the cast is valid, if it is then we go ahead.
if (DtoIsObjcLinkage(_to)) {
llvm::Function *kindOfClassFunc =
getRuntimeFunction(loc, gIR->module, "objc_opt_isKindOfClass");
// Get the object.
LLValue *obj = DtoRVal(val);
// Get class_t handle
LLValue *objTy = getNullPtr();
if (auto thandle = _to->isClassHandle()) {
objTy = gIR->objc.deref(thandle, getOpaquePtrType());
}
// objc_opt_isKindOfClass will check if id is null
// by itself, so we don't need to add an extra check.
// objc_opt_isKindOfClass(id) ? id : null
LLValue *objCastable = gIR->CreateCallOrInvoke(kindOfClassFunc, obj, objTy);
LLValue *ret = gIR->ir->CreateSelect(objCastable, obj, getNullPtr());
return new DImValue(_to, ret);
}
// call:
// Object _d_dynamic_cast(Object o, ClassInfo c)
llvm::Function *func =
getRuntimeFunction(loc, gIR->module, "_d_dynamic_cast");
[[maybe_unused]] LLFunctionType *funcTy = func->getFunctionType();
// Object o
LLValue *obj = DtoRVal(val);
assert(funcTy->getParamType(0) == obj->getType());
// ClassInfo c
TypeClass *to = static_cast<TypeClass *>(_to->toBasetype());
DtoResolveClass(to->sym);
LLValue *cinfo = getIrAggr(to->sym)->getClassInfoSymbol();
assert(funcTy->getParamType(1) == cinfo->getType());
// call it
LLValue *ret = gIR->CreateCallOrInvoke(func, obj, cinfo);
return new DImValue(_to, ret);
}
////////////////////////////////////////////////////////////////////////////////
DValue *DtoDynamicCastInterface(Loc loc, DValue *val, Type *_to) {
resolveObjectAndClassInfoClasses();
// Dynamic casting in Objective-C works differently from D.
// In this case we want to call the Objective-C runtime to first
// get a Class object from the `id`.
// Then check if class_conformsToProtocol returns true,
// if it does, then we can cast and return the casted value,
// otherwise return null.
if (DtoIsObjcLinkage(_to)) {
llvm::Function *getClassFunc =
getRuntimeFunction(loc, gIR->module, "object_getClass");
llvm::Function *kindOfProtocolFunc =
getRuntimeFunction(loc, gIR->module, "class_conformsToProtocol");
// id -> Class
LLValue *obj = DtoRVal(val);
LLValue *objClass = gIR->CreateCallOrInvoke(getClassFunc, obj);
// Get prototype_t handle
LLValue *protoTy = getNullPtr();
if (auto ifhndl = _to->isClassHandle()->isInterfaceDeclaration()) {
protoTy = gIR->objc.deref(ifhndl, getOpaquePtrType());
}
// Class && kindOfProtocolFunc(Class) ? id : null
LLValue *ret = gIR->ir->CreateSelect(
gIR->CreateCallOrInvoke(kindOfProtocolFunc, objClass, protoTy),
obj,
getNullPtr()
);
return new DImValue(_to, ret);
}
// call:
// Object _d_interface_cast(void* p, ClassInfo c)
llvm::Function *func =
getRuntimeFunction(loc, gIR->module, "_d_interface_cast");
// void* p
LLValue *ptr = DtoRVal(val);
// ClassInfo c
TypeClass *to = static_cast<TypeClass *>(_to->toBasetype());
DtoResolveClass(to->sym);
LLValue *cinfo = getIrAggr(to->sym)->getClassInfoSymbol();
// call it
LLValue *ret = gIR->CreateCallOrInvoke(func, ptr, cinfo);
return new DImValue(_to, ret);
}
////////////////////////////////////////////////////////////////////////////////
std::pair<llvm::Value *, llvm::Value *>
DtoVirtualFunctionPointer(DValue *inst, FuncDeclaration *fdecl) {
// sanity checks
assert(fdecl->isVirtual());
assert(!fdecl->isFinalFunc());
TypeClass *tc = inst->type->toBasetype()->isTypeClass();
assert(tc);
// slot 0 is always ClassInfo/Interface* unless it is a CPP class
assert(fdecl->vtblIndex > 0 ||
(fdecl->vtblIndex == 0 &&
inst->type->toBasetype()->isTypeClass()->sym->isCPPclass()));
// get instance
LLValue *vthis = DtoRVal(inst);
IF_LOG Logger::cout() << "vthis: " << *vthis << '\n';
const auto irtc = getIrType(tc->sym->type, true)->isClass();
const auto vtblType = irtc->getVtblType();
LLValue *vtable = vthis;
// get the vtbl for objects
vtable = DtoGEP(irtc->getMemoryLLType(), vthis, 0u, 0);
// load vtbl ptr
vtable = DtoLoad(LLPointerType::getUnqual(vtblType), vtable);
// index vtbl
const std::string name = fdecl->toChars();
const auto vtblname = name + "@vtbl";
LLValue *funcval =
DtoGEP(vtblType, vtable, 0, fdecl->vtblIndex, vtblname.c_str());
// load opaque pointer.
funcval = DtoAlignedLoad(vtblType->getElementType(), funcval);
// Because vtables are immutable, LLVM's !invariant.load
// can be applied (helps with devirtualization).
llvm::cast<llvm::LoadInst>(funcval)->setMetadata(
"invariant.load", llvm::MDNode::get(gIR->context(), {}));
IF_LOG Logger::cout() << "funcval: " << *funcval << '\n';
// postpone naming until after casting to get the name in call instructions
funcval->setName(name);
IF_LOG Logger::cout() << "funcval casted: " << *funcval << '\n';
return std::make_pair(funcval, vtable);
}