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
|
//===-- irtypestruct.cpp --------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "ir/irtypestruct.h"
#include "llvm/IR/DerivedTypes.h"
#include "aggregate.h"
#include "declaration.h"
#include "init.h"
#include "mtype.h"
#include "template.h"
#include "gen/irstate.h"
#include "gen/tollvm.h"
#include "gen/logger.h"
#include "gen/llvmhelpers.h"
#include "gen/dcompute/target.h"
#include "gen/dcompute/druntime.h"
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
IrTypeStruct::IrTypeStruct(StructDeclaration *sd)
: IrTypeAggr(sd), sd(sd), ts(static_cast<TypeStruct *>(sd->type)) {}
//////////////////////////////////////////////////////////////////////////////
IrTypeStruct *IrTypeStruct::get(StructDeclaration *sd) {
auto t = new IrTypeStruct(sd);
sd->type->ctype = t;
IF_LOG Logger::println("Building struct type %s @ %s", sd->toPrettyChars(),
sd->loc.toChars());
LOG_SCOPE;
// if it's a forward declaration, all bets are off, stick with the opaque
if (sd->sizeok != SIZEOKdone) {
return t;
}
t->packed = isPacked(sd);
// For ldc.dcomptetypes.Pointer!(uint n,T),
// emit { T addrspace(gIR->dcomputetarget->mapping[n])* }
llvm::Optional<DcomputePointer> p;
if (gIR->dcomputetarget && (p = toDcomputePointer(sd))) {
// Translate the virtual dcompute address space into the real one for
// the target
int realAS = gIR->dcomputetarget->mapping[p->addrspace];
llvm::SmallVector<LLType *, 1> body;
body.push_back(DtoMemType(p->type)->getPointerTo(realAS));
isaStruct(t->type)->setBody(body, t->packed);
VarGEPIndices v;
v[sd->fields[0]] = 0;
t->varGEPIndices = v;
} else {
AggrTypeBuilder builder(t->packed);
builder.addAggregate(sd);
builder.addTailPadding(sd->structsize);
isaStruct(t->type)->setBody(builder.defaultTypes(), t->packed);
t->varGEPIndices = builder.varGEPIndices();
}
IF_LOG Logger::cout() << "final struct type: " << *t->type << std::endl;
return t;
}
|