-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathirvar.cpp
257 lines (213 loc) · 8.57 KB
/
irvar.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
//===-- irvar.cpp ---------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "ir/irvar.h"
#include "dmd/declaration.h"
#include "dmd/errors.h"
#include "dmd/init.h"
#include "gen/dynamiccompile.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/pragma.h"
#include "gen/uda.h"
#include "ir/irdsymbol.h"
//////////////////////////////////////////////////////////////////////////////
LLValue *IrGlobal::getValue(bool define) {
if (!value) {
declare();
if (!define)
define = defineOnDeclare(V, /*isFunction=*/false);
}
if (define) {
if (V->storage_class & STCextern) {
// external
} else if (!gIR->funcGenStates.empty() &&
gIR->topfunc()->getLinkage() ==
LLGlobalValue::AvailableExternallyLinkage) {
// don't define globals while codegen'ing available_externally functions
} else {
auto gvar = llvm::dyn_cast<LLGlobalVariable>(value);
const bool isDefined = !gvar // bitcast pointer to a helper global
|| gvar->hasInitializer();
if (!isDefined)
this->define();
}
}
return value;
}
llvm::Type *IrGlobal::getType() {
return llvm::dyn_cast<llvm::GlobalVariable>(value)->getValueType();
}
void IrGlobal::declare() {
Logger::println("Declaring global: %s", V->toChars());
LOG_SCOPE
IF_LOG {
if (V->parent) {
Logger::println("parent: %s (%s)", V->parent->toChars(),
V->parent->kind());
} else {
Logger::println("parent: null");
}
}
assert(!value);
// If a const/immutable value has a proper initializer (not "= void"),
// it cannot be assigned again in a static constructor. Thus, we can
// emit it as read-only data.
// We also do so for forward-declared (extern) globals, just like clang.
const bool isLLConst = (V->isConst() || V->isImmutable()) &&
((V->_init && !V->_init->isVoidInitializer()) ||
(V->storage_class & STCextern));
const auto irMangle = getIRMangledName(V);
// Windows: for globals with `export` visibility, initialize the DLL storage
// class with dllimport unless the variable is defined in a root module
// (=> no extra indirection for other root modules, assuming *all* root
// modules will be linked together to one or more binaries).
// [Defining a global overrides its DLL storage class.]
bool useDLLImport = false;
if (global.params.targetTriple->isOSWindows()) {
// dllimport isn't supported for thread-local globals (MSVC++ neither)
if (!V->isThreadlocal()) {
// implicitly include extern(D) globals with -dllimport
useDLLImport =
(V->isExport() || V->_linkage == LINK::d) && dllimportDataSymbol(V);
}
}
// Since the type of a global must exactly match the type of its
// initializer, we cannot know the type until after we have emitted the
// latter (e.g. in case of unions, …). However, it is legal for the
// initializer to refer to the address of the variable. Thus, we first
// create a global with the generic type (note the assignment to
// value!), and in case we also do an initializer with a different type
// later, swap it out and replace any existing uses with bitcasts to the
// previous type.
LLGlobalVariable *gvar =
declareGlobal(V->loc, gIR->module, DtoMemType(V->type), irMangle,
isLLConst, V->isThreadlocal(), useDLLImport);
value = gvar;
if (V->llvmInternal == LLVMextern_weak)
gvar->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
// Set the alignment (it is important not to use type->alignsize because
// VarDeclarations can have an align() attribute independent of the type
// as well).
gvar->setAlignment(llvm::MaybeAlign(DtoAlignment(V)));
applyVarDeclUDAs(V, gvar);
if (dynamicCompileConst)
addDynamicCompiledVar(gIR, this);
IF_LOG Logger::cout() << *gvar << '\n';
}
void IrGlobal::define() {
Logger::println("Defining global: %s", V->toChars());
LOG_SCOPE
if (global.params.v.tls && V->isThreadlocal() &&
!(V->storage_class & STCtemp)) {
message("%s: `%s` is thread local", V->loc.toChars(), V->toChars());
}
LLConstant *initVal =
DtoConstInitializer(V->loc, V->type, V->_init, V->isCsymbol());
// Set the initializer, swapping out the variable if the types do not
// match.
auto gvar = llvm::cast<LLGlobalVariable>(value);
gvar = gIR->setGlobalVarInitializer(gvar, initVal, V);
value = gvar;
// dllexport isn't supported for thread-local globals (MSVC++ neither);
// don't let LLVM create a useless /EXPORT directive (yields the same linker
// error anyway when trying to dllimport).
if (gvar->hasDLLExportStorageClass() && V->isThreadlocal())
gvar->setDLLStorageClass(LLGlobalValue::DefaultStorageClass);
// If this global is used from a naked function, we need to create an
// artificial "use" for it, or it could be removed by the optimizer if
// the only reference to it is in inline asm.
// Also prevent linker-level dead-symbol-elimination from stripping
// special `rt_*` druntime symbol overrides (e.g., from executables linked
// against *shared* druntime; required at least for Apple's ld64 linker).
const auto name = gvar->getName();
if (nakedUse || name == "rt_options" || name == "rt_envvars_enabled" ||
name == "rt_cmdline_enabled") {
gIR->usedArray.push_back(gvar);
}
// Also set up the debug info.
gIR->DBuilder.EmitGlobalVariable(gvar, V);
IF_LOG Logger::cout() << *gvar << '\n';
}
//////////////////////////////////////////////////////////////////////////////
IrVar *getIrVar(VarDeclaration *decl) {
assert(isIrVarCreated(decl));
assert(decl->ir->irVar != NULL);
return decl->ir->irVar;
}
llvm::Value *getIrValue(VarDeclaration *decl) { return getIrVar(decl)->value; }
bool isIrVarCreated(VarDeclaration *decl) {
int t = decl->ir->type();
bool isIrVar = t == IrDsymbol::GlobalType || t == IrDsymbol::LocalType ||
t == IrDsymbol::ParamterType || t == IrDsymbol::FieldType;
assert(isIrVar || t == IrDsymbol::NotSet);
return isIrVar;
}
//////////////////////////////////////////////////////////////////////////////
IrGlobal *getIrGlobal(VarDeclaration *decl, bool create) {
if (!isIrGlobalCreated(decl) && create) {
assert(decl->ir->irGlobal == NULL);
decl->ir->irGlobal = new IrGlobal(decl);
decl->ir->m_type = IrDsymbol::GlobalType;
}
assert(decl->ir->irGlobal != NULL);
return decl->ir->irGlobal;
}
bool isIrGlobalCreated(VarDeclaration *decl) {
int t = decl->ir->type();
assert(t == IrDsymbol::GlobalType || t == IrDsymbol::NotSet);
return t == IrDsymbol::GlobalType;
}
//////////////////////////////////////////////////////////////////////////////
IrLocal *getIrLocal(VarDeclaration *decl, bool create) {
if (!isIrLocalCreated(decl) && create) {
assert(decl->ir->irLocal == NULL);
decl->ir->irLocal = new IrLocal(decl);
decl->ir->m_type = IrDsymbol::LocalType;
}
assert(decl->ir->irLocal != NULL);
return decl->ir->irLocal;
}
bool isIrLocalCreated(VarDeclaration *decl) {
int t = decl->ir->type();
assert(t == IrDsymbol::LocalType || t == IrDsymbol::ParamterType ||
t == IrDsymbol::NotSet);
return t == IrDsymbol::LocalType || t == IrDsymbol::ParamterType;
}
//////////////////////////////////////////////////////////////////////////////
IrParameter *getIrParameter(VarDeclaration *decl, bool create) {
if (!isIrParameterCreated(decl) && create) {
assert(decl->ir->irParam == NULL);
decl->ir->irParam = new IrParameter(decl);
decl->ir->m_type = IrDsymbol::ParamterType;
}
return decl->ir->irParam;
}
bool isIrParameterCreated(VarDeclaration *decl) {
int t = decl->ir->type();
assert(t == IrDsymbol::ParamterType || t == IrDsymbol::NotSet);
return t == IrDsymbol::ParamterType;
}
//////////////////////////////////////////////////////////////////////////////
IrField *getIrField(VarDeclaration *decl, bool create) {
if (!isIrFieldCreated(decl) && create) {
assert(decl->ir->irField == NULL);
decl->ir->irField = new IrField(decl);
decl->ir->m_type = IrDsymbol::FieldType;
}
assert(decl->ir->irField != NULL);
return decl->ir->irField;
}
bool isIrFieldCreated(VarDeclaration *decl) {
int t = decl->ir->type();
assert(t == IrDsymbol::FieldType || t == IrDsymbol::NotSet);
return t == IrDsymbol::FieldType;
}