-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathirstruct.cpp
196 lines (162 loc) · 5.74 KB
/
irstruct.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
//===-- irstruct.cpp ------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "dmd/errors.h"
#include "dmd/mangle.h"
#include "dmd/mtype.h"
#include "dmd/template.h"
#include "gen/irstate.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/rttibuilder.h"
#include "gen/runtime.h"
#include "gen/structs.h"
#include "gen/tollvm.h"
#include "gen/typinf.h"
#include "ir/iraggr.h"
#include "ir/irtypeclass.h"
using namespace dmd;
namespace {
LLStructType* getTypeInfoStructMemType() {
Type *t = getStructTypeInfoType();
IrTypeClass *tc = getIrType(t, true)->isClass();
assert(tc && "invalid TypeInfo_Struct type");
return llvm::cast<LLStructType>(tc->getMemoryLLType());
}
}
LLGlobalVariable* IrStruct::getTypeInfoSymbol(bool define) {
if (!typeInfo) {
OutBuffer mangledName;
mangledName.writestring("TypeInfo_S");
mangleToBuffer(aggrdecl, mangledName);
const auto length = mangledName.length();
mangledName.prependstring(("_D" + std::to_string(length)).c_str());
mangledName.writestring("6__initZ");
const auto irMangle = getIRMangledVarName(mangledName.peekChars(), LINK::d);
// We need to keep the symbol mutable as the type is not declared as
// immutable on the D side, and e.g. synchronized() can be used on the
// implicit monitor.
const bool isConstant = false;
// Struct TypeInfos are emitted into each referencing CU.
const bool useDLLImport = false;
typeInfo =
declareGlobal(aggrdecl->loc, gIR->module, getTypeInfoStructMemType(),
irMangle, isConstant, false, useDLLImport);
emitTypeInfoMetadata(typeInfo, aggrdecl->type);
if (!define)
define = defineOnDeclare(aggrdecl, /*isFunction=*/false);
}
if (define) {
auto init = getTypeInfoInit();
if (!typeInfo->hasInitializer())
defineGlobal(typeInfo, init, aggrdecl);
}
return typeInfo;
}
LLConstant *IrStruct::getTypeInfoInit() {
// The upstream implementation is in dmd/todt.d,
// TypeInfoDtVisitor.visit(TypeInfoStructDeclaration).
if (constTypeInfo) {
return constTypeInfo;
}
auto sd = aggrdecl->isStructDeclaration();
IF_LOG Logger::println("Defining TypeInfo for struct: %s", sd->toChars());
LOG_SCOPE;
// we need (dummy) TypeInfos for opaque structs too
const bool isOpaque = !sd->members;
// make sure xtoHash/xopEquals/xopCmp etc. are semantically analyzed
if (!isOpaque && sd->semanticRun() < PASS::semantic3done) {
Logger::println(
"Struct hasn't had semantic3 yet, calling semanticTypeInfoMembers()");
semanticTypeInfoMembers(sd);
}
TypeStruct *ts = sd->type->isTypeStruct();
// check declaration in object.d
const auto structTypeInfoType = getStructTypeInfoType();
const auto structTypeInfoDecl = Type::typeinfostruct;
// For x86_64 (except Win64) and AAPCS64 targets, class TypeInfo_Struct
// contains 2 additional fields (m_arg1/m_arg2) which are used for the
// TypeInfo-based core.stdc.stdarg.va_arg implementations in druntime.
const auto &triple = *global.params.targetTriple;
const auto arch = triple.getArch();
const bool withArgTypes =
(arch == llvm::Triple::x86_64 && !triple.isOSWindows()) ||
(!triple.isOSDarwin() && // Apple uses a simpler scheme
(arch == llvm::Triple::aarch64 || arch == llvm::Triple::aarch64_be));
const unsigned expectedFields = 11 + (withArgTypes ? 2 : 0);
const unsigned actualFields =
structTypeInfoDecl->fields.length -
1; // union of xdtor/xdtorti counts as 2 overlapping fields
if (actualFields != expectedFields) {
error(Loc(), "Unexpected number of `object.TypeInfo_Struct` fields; "
"druntime version does not match compiler");
fatal();
}
RTTIBuilder b(structTypeInfoType);
// string mangledName
if (isOpaque) {
b.push_null_void_array();
} else {
b.push_string(ts->deco);
}
// void[] m_init
// The protocol is to write a null pointer for zero-initialized structs.
// The length field is always needed for tsize().
if (isOpaque) {
b.push_null_void_array();
} else {
llvm::Constant *initPtr;
if (isZeroInit(ts)) {
initPtr = getNullPtr();
} else {
initPtr = getInitSymbol();
}
b.push_void_array(sd->size(Loc()), initPtr);
}
// function xtoHash
b.push_funcptr(isOpaque ? nullptr : sd->xhash);
// function xopEquals
b.push_funcptr(isOpaque ? nullptr : sd->xeq);
// function xopCmp
b.push_funcptr(isOpaque ? nullptr : sd->xcmp);
// function xtoString
b.push_funcptr(isOpaque ? nullptr : search_toString(sd));
// StructFlags m_flags
b.push_uint(!isOpaque && hasPointers(ts) ? 1 : 0);
// function xdtor/xdtorti
b.push_funcptr(isOpaque ? nullptr : sd->tidtor);
// function xpostblit
FuncDeclaration *xpostblit = isOpaque ? nullptr : sd->postblit;
if (xpostblit && (xpostblit->storage_class & STCdisable)) {
xpostblit = nullptr;
}
b.push_funcptr(xpostblit);
// uint m_align
b.push_uint(isOpaque ? 0 : DtoAlignment(ts));
if (withArgTypes) {
// TypeInfo m_arg1
// TypeInfo m_arg2
for (unsigned i = 0; i < 2; i++) {
if (auto t = isOpaque ? nullptr : sd->argType(i)) {
t = merge(t);
b.push_typeinfo(t);
} else {
b.push_null(getTypeInfoType());
}
}
}
// immutable(void)* m_RTInfo
if (!isOpaque && sd->getRTInfo) {
b.push(toConstElem(sd->getRTInfo, gIR));
} else {
b.push_size_as_vp(!isOpaque && hasPointers(ts) ? 1 : 0);
}
constTypeInfo = b.get_constant(getTypeInfoStructMemType());
return constTypeInfo;
}