/
declarations.cpp
494 lines (396 loc) · 16.5 KB
/
declarations.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
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
//===-- declarations.cpp --------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "aggregate.h"
#include "declaration.h"
#include "enum.h"
#include "id.h"
#include "init.h"
#include "rmem.h"
#include "template.h"
#include "gen/classes.h"
#include "gen/functions.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/tollvm.h"
#include "ir/irtype.h"
#include "ir/irvar.h"
#include "llvm/ADT/SmallString.h"
//////////////////////////////////////////////////////////////////////////////
// FIXME: Integrate these functions
void TypeInfoDeclaration_codegen(TypeInfoDeclaration *decl, IRState* p);
void TypeInfoClassDeclaration_codegen(TypeInfoDeclaration *decl, IRState* p);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
class CodegenVisitor : public Visitor {
IRState *irs;
public:
CodegenVisitor(IRState *irs) : irs(irs) { }
//////////////////////////////////////////////////////////////////////////
// Import all functions from class Visitor
using Visitor::visit;
//////////////////////////////////////////////////////////////////////////
void visit(Dsymbol *sym) LLVM_OVERRIDE {
IF_LOG Logger::println("Ignoring Dsymbol::codegen for %s", sym->toPrettyChars());
}
//////////////////////////////////////////////////////////////////////////
void visit(InterfaceDeclaration *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("InterfaceDeclaration::codegen: '%s'", decl->toPrettyChars());
LOG_SCOPE
if (decl->ir.isDefined()) return;
if (decl->type->ty == Terror)
{
error(decl->loc, "had semantic errors when compiling");
decl->ir.setDefined();
return;
}
if (decl->members && decl->symtab)
{
DtoResolveClass(decl);
decl->ir.setDefined();
// Emit any members (e.g. final functions).
for (Dsymbols::iterator I = decl->members->begin(),
E = decl->members->end();
I != E; ++I)
{
(*I)->accept(this);
}
// Emit TypeInfo.
DtoTypeInfoOf(decl->type);
// Define __InterfaceZ.
IrAggr *ir = getIrAggr(decl);
llvm::GlobalVariable *interfaceZ = ir->getClassInfoSymbol();
interfaceZ->setInitializer(ir->getClassInfoInit());
LinkageWithCOMDAT lwc = DtoLinkage(decl);
interfaceZ->setLinkage(lwc.first);
if (lwc.second) SET_COMDAT(interfaceZ, gIR->module);
}
}
//////////////////////////////////////////////////////////////////////////
void visit(StructDeclaration *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("StructDeclaration::codegen: '%s'", decl->toPrettyChars());
LOG_SCOPE
if (decl->ir.isDefined()) return;
if (decl->type->ty == Terror)
{
error(decl->loc, "had semantic errors when compiling");
decl->ir.setDefined();
return;
}
if (decl->members && decl->symtab)
{
DtoResolveStruct(decl);
decl->ir.setDefined();
for (Dsymbols::iterator I = decl->members->begin(),
E = decl->members->end();
I != E; ++I)
{
(*I)->accept(this);
}
// Define the __initZ symbol.
IrAggr *ir = getIrAggr(decl);
llvm::GlobalVariable *initZ = ir->getInitSymbol();
initZ->setInitializer(ir->getDefaultInit());
LinkageWithCOMDAT lwc = DtoLinkage(decl);
initZ->setLinkage(lwc.first);
if (lwc.second) SET_COMDAT(initZ, gIR->module);
// emit typeinfo
DtoTypeInfoOf(decl->type);
// Emit __xopEquals/__xopCmp/__xtoHash.
if (decl->xeq && decl->xeq != decl->xerreq)
decl->xeq->accept(this);
if (decl->xcmp && decl->xcmp != decl->xerrcmp)
decl->xcmp->accept(this);
if (decl->xhash)
decl->xhash->accept(this);
}
}
//////////////////////////////////////////////////////////////////////////
void visit(ClassDeclaration *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("ClassDeclaration::codegen: '%s'", decl->toPrettyChars());
LOG_SCOPE
if (decl->ir.isDefined()) return;
if (decl->type->ty == Terror)
{
error(decl->loc, "had semantic errors when compiling");
decl->ir.setDefined();
return;
}
if (decl->members && decl->symtab)
{
DtoResolveClass(decl);
decl->ir.setDefined();
for (Dsymbols::iterator I = decl->members->begin(),
E = decl->members->end();
I != E; ++I)
{
(*I)->accept(this);
}
IrAggr *ir = getIrAggr(decl);
const LinkageWithCOMDAT lwc = DtoLinkage(decl);
llvm::GlobalVariable *initZ = ir->getInitSymbol();
initZ->setInitializer(ir->getDefaultInit());
initZ->setLinkage(lwc.first);
if (lwc.second) SET_COMDAT(initZ, gIR->module);
llvm::GlobalVariable *vtbl = ir->getVtblSymbol();
vtbl->setInitializer(ir->getVtblInit());
vtbl->setLinkage(lwc.first);
if (lwc.second) SET_COMDAT(vtbl, gIR->module);
llvm::GlobalVariable *classZ = ir->getClassInfoSymbol();
classZ->setInitializer(ir->getClassInfoInit());
classZ->setLinkage(lwc.first);
if (lwc.second) SET_COMDAT(classZ, gIR->module);
// No need to do TypeInfo here, it is <name>__classZ for classes in D2.
}
}
//////////////////////////////////////////////////////////////////////////
void visit(TupleDeclaration *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("TupleDeclaration::codegen(): '%s'", decl->toPrettyChars());
LOG_SCOPE
if (decl->ir.isDefined()) return;
decl->ir.setDefined();
assert(decl->isexp);
assert(decl->objects);
for (Objects::iterator I = decl->objects->begin(),
E = decl->objects->end();
I != E; ++I)
{
DsymbolExp *exp = static_cast<DsymbolExp *>(*I);
assert(exp->op == TOKdsymbol);
exp->s->accept(this);
}
}
//////////////////////////////////////////////////////////////////////////
void visit(VarDeclaration *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("VarDeclaration::codegen(): '%s'", decl->toPrettyChars());
LOG_SCOPE;
if (decl->ir.isDefined()) return;
if (decl->type->ty == Terror)
{
error(decl->loc, "had semantic errors when compiling");
decl->ir.setDefined();
return;
}
DtoResolveVariable(decl);
decl->ir.setDefined();
// just forward aliases
if (decl->aliassym)
{
Logger::println("alias sym");
decl->toAlias()->accept(this);
return;
}
// global variable
if (decl->isDataseg())
{
Logger::println("data segment");
assert(!(decl->storage_class & STCmanifest) &&
"manifest constant being codegen'd!");
IrGlobal *irGlobal = getIrGlobal(decl);
llvm::GlobalVariable *gvar = llvm::cast<llvm::GlobalVariable>(irGlobal->value);
assert(gvar && "DtoResolveVariable should have created value");
const LinkageWithCOMDAT lwc = DtoLinkage(decl);
// Check if we are defining or just declaring the global in this module.
if (!(decl->storage_class & STCextern))
{
// Build the initializer. Might use this->ir.irGlobal->value!
LLConstant *initVal = DtoConstInitializer(decl->loc, decl->type, decl->init);
// In case of type mismatch, swap out the variable.
if (initVal->getType() != gvar->getType()->getElementType())
{
llvm::GlobalVariable* newGvar = getOrCreateGlobal(decl->loc,
irs->module, initVal->getType(), gvar->isConstant(),
lwc.first, 0,
"", // We take on the name of the old global below.
gvar->isThreadLocal());
if (lwc.second) SET_COMDAT(newGvar, gIR->module);
newGvar->setAlignment(gvar->getAlignment());
newGvar->takeName(gvar);
llvm::Constant* newValue =
llvm::ConstantExpr::getBitCast(newGvar, gvar->getType());
gvar->replaceAllUsesWith(newValue);
gvar->eraseFromParent();
gvar = newGvar;
irGlobal->value = newGvar;
}
// Now, set the initializer.
assert(!irGlobal->constInit);
irGlobal->constInit = initVal;
gvar->setInitializer(initVal);
gvar->setLinkage(lwc.first);
if (lwc.second) SET_COMDAT(gvar, gIR->module);
// Also set up the debug info.
irs->DBuilder.EmitGlobalVariable(gvar, decl);
}
// 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.
if (irGlobal->nakedUse)
irs->usedArray.push_back(DtoBitCast(gvar, getVoidPtrType()));
IF_LOG Logger::cout() << *gvar << '\n';
}
}
//////////////////////////////////////////////////////////////////////////
void visit(EnumDeclaration *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("Ignoring EnumDeclaration::codegen: '%s'", decl->toPrettyChars());
if (decl->type->ty == Terror)
{ error(decl->loc, "had semantic errors when compiling");
return;
}
}
//////////////////////////////////////////////////////////////////////////
void visit(FuncDeclaration *decl) LLVM_OVERRIDE {
// don't touch function aliases, they don't contribute any new symbols
if (!decl->isFuncAliasDeclaration())
{
DtoDefineFunction(decl);
}
}
//////////////////////////////////////////////////////////////////////////
void visit(TemplateInstance *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("TemplateInstance::codegen: '%s'", decl->toPrettyChars());
LOG_SCOPE
if (decl->ir.isDefined()) return;
decl->ir.setDefined();
// FIXME: This is #673 all over again.
if (!decl->needsCodegen()) return;
if (!decl->errors && decl->members)
{
for (Dsymbols::iterator I = decl->members->begin(),
E = decl->members->end();
I != E; ++I)
{
(*I)->accept(this);
}
}
}
//////////////////////////////////////////////////////////////////////////
void visit(TemplateMixin *decl) LLVM_OVERRIDE {
IF_LOG Logger::println("TemplateInstance::codegen: '%s'", decl->toPrettyChars());
LOG_SCOPE
if (decl->ir.isDefined()) return;
decl->ir.setDefined();
if (!decl->errors && decl->members)
{
for (Dsymbols::iterator I = decl->members->begin(),
E = decl->members->end();
I != E; ++I)
{
(*I)->accept(this);
}
}
}
//////////////////////////////////////////////////////////////////////////
void visit(AttribDeclaration *decl) LLVM_OVERRIDE {
Dsymbols *d = decl->include(NULL, NULL);
if (d)
{
for (Dsymbols::iterator I = d->begin(),
E = d->end();
I != E; ++I)
{
(*I)->accept(this);
}
}
}
//////////////////////////////////////////////////////////////////////////
void visit(PragmaDeclaration *decl) LLVM_OVERRIDE {
if (decl->ident == Id::lib)
{
assert(decl->args && decl->args->dim == 1);
Expression *e = static_cast<Expression *>(decl->args->data[0]);
assert(e->op == TOKstring);
StringExp *se = static_cast<StringExp *>(e);
size_t nameLen = se->len;
#if LDC_LLVM_VER >= 305
if (global.params.targetTriple.isWindowsGNUEnvironment())
#else
if (global.params.targetTriple.getOS() == llvm::Triple::MinGW32)
#endif
{
if (nameLen > 4 &&
!memcmp(static_cast<char*>(se->string) + nameLen - 4, ".lib", 4))
{
// On MinGW, strip the .lib suffix, if any, to improve
// compatibility with code written for DMD (we pass the name to GCC
// via -l, just as on Posix).
nameLen -= 4;
}
if (nameLen >= 7 && !memcmp(se->string, "shell32", 7))
{
// Another DMD compatibility kludge: Ignore
// pragma(lib, "shell32.lib"), it is implicitly provided by
// MinGW.
return;
}
}
#if LDC_LLVM_VER >= 303
// With LLVM 3.3 or later we can place the library name in the object
// file. This seems to be supported only on Windows.
#if LDC_LLVM_VER >= 305
if (global.params.targetTriple.isWindowsMSVCEnvironment())
#else
if (global.params.targetTriple.getOS() == llvm::Triple::Win32)
#endif
{
llvm::SmallString<24> LibName(llvm::StringRef(static_cast<const char *>(se->string), nameLen));
// Win32: /DEFAULTLIB:"curl"
if (LibName.endswith(".a"))
LibName = LibName.substr(0, LibName.size()-2);
if (LibName.endswith(".lib"))
LibName = LibName.substr(0, LibName.size()-4);
llvm::SmallString<24> tmp("/DEFAULTLIB:\"");
tmp.append(LibName);
tmp.append("\"");
LibName = tmp;
// Embedd library name as linker option in object file
#if LDC_LLVM_VER >= 306
llvm::Metadata *Value = llvm::MDString::get(gIR->context(), LibName);
gIR->LinkerMetadataArgs.push_back(llvm::MDNode::get(gIR->context(), Value));
#else
llvm::Value *Value = llvm::MDString::get(gIR->context(), LibName);
gIR->LinkerMetadataArgs.push_back(llvm::MDNode::get(gIR->context(), Value));
#endif
}
else
#endif
{
size_t const n = nameLen + 3;
char *arg = static_cast<char *>(mem.xmalloc(n));
arg[0] = '-';
arg[1] = 'l';
memcpy(arg + 2, se->string, nameLen);
arg[n-1] = 0;
global.params.linkswitches->push(arg);
}
}
visit(static_cast<AttribDeclaration *>(decl));
}
//////////////////////////////////////////////////////////////////////////
void visit(TypeInfoDeclaration *decl) LLVM_OVERRIDE {
TypeInfoDeclaration_codegen(decl, irs);
}
//////////////////////////////////////////////////////////////////////////
void visit(TypeInfoClassDeclaration *decl) LLVM_OVERRIDE {
TypeInfoClassDeclaration_codegen(decl, irs);
}
};
//////////////////////////////////////////////////////////////////////////////
void Declaration_codegen(Dsymbol *decl)
{
CodegenVisitor v(gIR);
decl->accept(&v);
}
void Declaration_codegen(Dsymbol *decl, IRState *irs)
{
CodegenVisitor v(irs);
decl->accept(&v);
}
//////////////////////////////////////////////////////////////////////////////