1//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with code generation of C++ declarations
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CGCXXABI.h"
16#include "CGObjCRuntime.h"
17#include "clang/Frontend/CodeGenOptions.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/IR/Intrinsics.h"
20
21using namespace clang;
22using namespace CodeGen;
23
24static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
25                         llvm::Constant *DeclPtr) {
26  assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
27  assert(!D.getType()->isReferenceType() &&
28         "Should not call EmitDeclInit on a reference!");
29
30  ASTContext &Context = CGF.getContext();
31
32  CharUnits alignment = Context.getDeclAlign(&D);
33  QualType type = D.getType();
34  LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
35
36  const Expr *Init = D.getInit();
37  switch (CGF.getEvaluationKind(type)) {
38  case TEK_Scalar: {
39    CodeGenModule &CGM = CGF.CGM;
40    if (lv.isObjCStrong())
41      CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
42                                                DeclPtr, D.getTLSKind());
43    else if (lv.isObjCWeak())
44      CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
45                                              DeclPtr);
46    else
47      CGF.EmitScalarInit(Init, &D, lv, false);
48    return;
49  }
50  case TEK_Complex:
51    CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
52    return;
53  case TEK_Aggregate:
54    CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
55                                          AggValueSlot::DoesNotNeedGCBarriers,
56                                                  AggValueSlot::IsNotAliased));
57    return;
58  }
59  llvm_unreachable("bad evaluation kind");
60}
61
62/// Emit code to cause the destruction of the given variable with
63/// static storage duration.
64static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
65                            llvm::Constant *addr) {
66  CodeGenModule &CGM = CGF.CGM;
67
68  // FIXME:  __attribute__((cleanup)) ?
69
70  QualType type = D.getType();
71  QualType::DestructionKind dtorKind = type.isDestructedType();
72
73  switch (dtorKind) {
74  case QualType::DK_none:
75    return;
76
77  case QualType::DK_cxx_destructor:
78    break;
79
80  case QualType::DK_objc_strong_lifetime:
81  case QualType::DK_objc_weak_lifetime:
82    // We don't care about releasing objects during process teardown.
83    assert(!D.getTLSKind() && "should have rejected this");
84    return;
85  }
86
87  llvm::Constant *function;
88  llvm::Constant *argument;
89
90  // Special-case non-array C++ destructors, where there's a function
91  // with the right signature that we can just call.
92  const CXXRecordDecl *record = 0;
93  if (dtorKind == QualType::DK_cxx_destructor &&
94      (record = type->getAsCXXRecordDecl())) {
95    assert(!record->hasTrivialDestructor());
96    CXXDestructorDecl *dtor = record->getDestructor();
97
98    function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
99    argument = llvm::ConstantExpr::getBitCast(
100        addr, CGF.getTypes().ConvertType(type)->getPointerTo());
101
102  // Otherwise, the standard logic requires a helper function.
103  } else {
104    function = CodeGenFunction(CGM)
105        .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind),
106                               CGF.needsEHCleanup(dtorKind), &D);
107    argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
108  }
109
110  CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument);
111}
112
113/// Emit code to cause the variable at the given address to be considered as
114/// constant from this point onwards.
115static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
116                              llvm::Constant *Addr) {
117  // Don't emit the intrinsic if we're not optimizing.
118  if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
119    return;
120
121  // Grab the llvm.invariant.start intrinsic.
122  llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
123  llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
124
125  // Emit a call with the size in bytes of the object.
126  CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
127  uint64_t Width = WidthChars.getQuantity();
128  llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
129                           llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
130  CGF.Builder.CreateCall(InvariantStart, Args);
131}
132
133void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
134                                               llvm::Constant *DeclPtr,
135                                               bool PerformInit) {
136
137  const Expr *Init = D.getInit();
138  QualType T = D.getType();
139
140  if (!T->isReferenceType()) {
141    if (PerformInit)
142      EmitDeclInit(*this, D, DeclPtr);
143    if (CGM.isTypeConstant(D.getType(), true))
144      EmitDeclInvariant(*this, D, DeclPtr);
145    else
146      EmitDeclDestroy(*this, D, DeclPtr);
147    return;
148  }
149
150  assert(PerformInit && "cannot have constant initializer which needs "
151         "destruction for reference");
152  unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
153  RValue RV = EmitReferenceBindingToExpr(Init);
154  EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
155}
156
157static llvm::Function *
158CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
159                                   llvm::FunctionType *ty,
160                                   const Twine &name,
161                                   bool TLS = false);
162
163/// Create a stub function, suitable for being passed to atexit,
164/// which passes the given address to the given destructor function.
165static llvm::Constant *createAtExitStub(CodeGenModule &CGM, const VarDecl &VD,
166                                        llvm::Constant *dtor,
167                                        llvm::Constant *addr) {
168  // Get the destructor function type, void(*)(void).
169  llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
170  SmallString<256> FnName;
171  {
172    llvm::raw_svector_ostream Out(FnName);
173    CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
174  }
175  llvm::Function *fn =
176      CreateGlobalInitOrDestructFunction(CGM, ty, FnName.str());
177
178  CodeGenFunction CGF(CGM);
179
180  CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn,
181                    CGM.getTypes().arrangeNullaryFunction(), FunctionArgList(),
182                    SourceLocation());
183
184  llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
185
186 // Make sure the call and the callee agree on calling convention.
187  if (llvm::Function *dtorFn =
188        dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
189    call->setCallingConv(dtorFn->getCallingConv());
190
191  CGF.FinishFunction();
192
193  return fn;
194}
195
196/// Register a global destructor using the C atexit runtime function.
197void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD,
198                                                   llvm::Constant *dtor,
199                                                   llvm::Constant *addr) {
200  // Create a function which calls the destructor.
201  llvm::Constant *dtorStub = createAtExitStub(CGM, VD, dtor, addr);
202
203  // extern "C" int atexit(void (*f)(void));
204  llvm::FunctionType *atexitTy =
205    llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
206
207  llvm::Constant *atexit =
208    CGM.CreateRuntimeFunction(atexitTy, "atexit");
209  if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
210    atexitFn->setDoesNotThrow();
211
212  EmitNounwindRuntimeCall(atexit, dtorStub);
213}
214
215void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
216                                         llvm::GlobalVariable *DeclPtr,
217                                         bool PerformInit) {
218  // If we've been asked to forbid guard variables, emit an error now.
219  // This diagnostic is hard-coded for Darwin's use case;  we can find
220  // better phrasing if someone else needs it.
221  if (CGM.getCodeGenOpts().ForbidGuardVariables)
222    CGM.Error(D.getLocation(),
223              "this initialization requires a guard variable, which "
224              "the kernel does not support");
225
226  CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
227}
228
229static llvm::Function *
230CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
231                                   llvm::FunctionType *FTy,
232                                   const Twine &Name, bool TLS) {
233  llvm::Function *Fn =
234    llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
235                           Name, &CGM.getModule());
236  if (!CGM.getLangOpts().AppleKext && !TLS) {
237    // Set the section if needed.
238    if (const char *Section =
239          CGM.getTarget().getStaticInitSectionSpecifier())
240      Fn->setSection(Section);
241  }
242
243  Fn->setCallingConv(CGM.getRuntimeCC());
244
245  if (!CGM.getLangOpts().Exceptions)
246    Fn->setDoesNotThrow();
247
248  if (CGM.getSanOpts().Address)
249    Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
250  if (CGM.getSanOpts().Thread)
251    Fn->addFnAttr(llvm::Attribute::SanitizeThread);
252  if (CGM.getSanOpts().Memory)
253    Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
254
255  return Fn;
256}
257
258void
259CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
260                                            llvm::GlobalVariable *Addr,
261                                            bool PerformInit) {
262  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
263  SmallString<256> FnName;
264  {
265    llvm::raw_svector_ostream Out(FnName);
266    getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
267  }
268
269  // Create a variable initialization function.
270  llvm::Function *Fn =
271      CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str());
272
273  CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
274                                                          PerformInit);
275
276  if (D->getTLSKind()) {
277    // FIXME: Should we support init_priority for thread_local?
278    // FIXME: Ideally, initialization of instantiated thread_local static data
279    // members of class templates should not trigger initialization of other
280    // entities in the TU.
281    // FIXME: We only need to register one __cxa_thread_atexit function for the
282    // entire TU.
283    CXXThreadLocalInits.push_back(Fn);
284  } else if (D->hasAttr<InitPriorityAttr>()) {
285    unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
286    OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
287    PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
288    DelayedCXXInitPosition.erase(D);
289  } else if (D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
290             D->getTemplateSpecializationKind() != TSK_Undeclared) {
291    // C++ [basic.start.init]p2:
292    //   Definitions of explicitly specialized class template static data
293    //   members have ordered initialization. Other class template static data
294    //   members (i.e., implicitly or explicitly instantiated specializations)
295    //   have unordered initialization.
296    //
297    // As a consequence, we can put them into their own llvm.global_ctors entry.
298    // This should allow GlobalOpt to fire more often, and allow us to implement
299    // the Microsoft C++ ABI, which uses COMDAT elimination to avoid double
300    // initializaiton.
301    AddGlobalCtor(Fn);
302    DelayedCXXInitPosition.erase(D);
303  } else {
304    llvm::DenseMap<const Decl *, unsigned>::iterator I =
305      DelayedCXXInitPosition.find(D);
306    if (I == DelayedCXXInitPosition.end()) {
307      CXXGlobalInits.push_back(Fn);
308    } else {
309      assert(CXXGlobalInits[I->second] == 0);
310      CXXGlobalInits[I->second] = Fn;
311      DelayedCXXInitPosition.erase(I);
312    }
313  }
314}
315
316void CodeGenModule::EmitCXXThreadLocalInitFunc() {
317  llvm::Function *InitFn = 0;
318  if (!CXXThreadLocalInits.empty()) {
319    // Generate a guarded initialization function.
320    llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
321    InitFn = CreateGlobalInitOrDestructFunction(*this, FTy, "__tls_init",
322                                                /*TLS*/ true);
323    llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
324        getModule(), Int8Ty, false, llvm::GlobalVariable::InternalLinkage,
325        llvm::ConstantInt::get(Int8Ty, 0), "__tls_guard");
326    Guard->setThreadLocal(true);
327    CodeGenFunction(*this)
328        .GenerateCXXGlobalInitFunc(InitFn, CXXThreadLocalInits, Guard);
329  }
330
331  getCXXABI().EmitThreadLocalInitFuncs(CXXThreadLocals, InitFn);
332
333  CXXThreadLocalInits.clear();
334  CXXThreadLocals.clear();
335}
336
337void
338CodeGenModule::EmitCXXGlobalInitFunc() {
339  while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
340    CXXGlobalInits.pop_back();
341
342  if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
343    return;
344
345  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
346
347
348  // Create our global initialization function.
349  if (!PrioritizedCXXGlobalInits.empty()) {
350    SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
351    llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
352                         PrioritizedCXXGlobalInits.end());
353    // Iterate over "chunks" of ctors with same priority and emit each chunk
354    // into separate function. Note - everything is sorted first by priority,
355    // second - by lex order, so we emit ctor functions in proper order.
356    for (SmallVectorImpl<GlobalInitData >::iterator
357           I = PrioritizedCXXGlobalInits.begin(),
358           E = PrioritizedCXXGlobalInits.end(); I != E; ) {
359      SmallVectorImpl<GlobalInitData >::iterator
360        PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
361
362      LocalCXXGlobalInits.clear();
363      unsigned Priority = I->first.priority;
364      // Compute the function suffix from priority. Prepend with zeroes to make
365      // sure the function names are also ordered as priorities.
366      std::string PrioritySuffix = llvm::utostr(Priority);
367      // Priority is always <= 65535 (enforced by sema)..
368      PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
369      llvm::Function *Fn =
370        CreateGlobalInitOrDestructFunction(*this, FTy,
371                                           "_GLOBAL__I_" + PrioritySuffix);
372
373      for (; I < PrioE; ++I)
374        LocalCXXGlobalInits.push_back(I->second);
375
376      CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
377      AddGlobalCtor(Fn, Priority);
378    }
379  }
380
381  llvm::Function *Fn =
382    CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
383
384  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
385  AddGlobalCtor(Fn);
386
387  CXXGlobalInits.clear();
388  PrioritizedCXXGlobalInits.clear();
389}
390
391void CodeGenModule::EmitCXXGlobalDtorFunc() {
392  if (CXXGlobalDtors.empty())
393    return;
394
395  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
396
397  // Create our global destructor function.
398  llvm::Function *Fn =
399    CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
400
401  CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
402  AddGlobalDtor(Fn);
403}
404
405/// Emit the code necessary to initialize the given global variable.
406void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
407                                                       const VarDecl *D,
408                                                 llvm::GlobalVariable *Addr,
409                                                       bool PerformInit) {
410  // Check if we need to emit debug info for variable initializer.
411  if (D->hasAttr<NoDebugAttr>())
412    DebugInfo = NULL; // disable debug info indefinitely for this function
413
414  StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
415                getTypes().arrangeNullaryFunction(),
416                FunctionArgList(), D->getInit()->getExprLoc());
417
418  // Use guarded initialization if the global variable is weak. This
419  // occurs for, e.g., instantiated static data members and
420  // definitions explicitly marked weak.
421  if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
422      Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
423    EmitCXXGuardedInit(*D, Addr, PerformInit);
424  } else {
425    EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
426  }
427
428  FinishFunction();
429}
430
431void
432CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
433                                           ArrayRef<llvm::Constant *> Decls,
434                                           llvm::GlobalVariable *Guard) {
435  StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
436                getTypes().arrangeNullaryFunction(),
437                FunctionArgList(), SourceLocation());
438
439  llvm::BasicBlock *ExitBlock = 0;
440  if (Guard) {
441    // If we have a guard variable, check whether we've already performed these
442    // initializations. This happens for TLS initialization functions.
443    llvm::Value *GuardVal = Builder.CreateLoad(Guard);
444    llvm::Value *Uninit = Builder.CreateIsNull(GuardVal, "guard.uninitialized");
445    // Mark as initialized before initializing anything else. If the
446    // initializers use previously-initialized thread_local vars, that's
447    // probably supposed to be OK, but the standard doesn't say.
448    Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(), 1), Guard);
449    llvm::BasicBlock *InitBlock = createBasicBlock("init");
450    ExitBlock = createBasicBlock("exit");
451    Builder.CreateCondBr(Uninit, InitBlock, ExitBlock);
452    EmitBlock(InitBlock);
453  }
454
455  RunCleanupsScope Scope(*this);
456
457  // When building in Objective-C++ ARC mode, create an autorelease pool
458  // around the global initializers.
459  if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
460    llvm::Value *token = EmitObjCAutoreleasePoolPush();
461    EmitObjCAutoreleasePoolCleanup(token);
462  }
463
464  for (unsigned i = 0, e = Decls.size(); i != e; ++i)
465    if (Decls[i])
466      EmitRuntimeCall(Decls[i]);
467
468  Scope.ForceCleanup();
469
470  if (ExitBlock) {
471    Builder.CreateBr(ExitBlock);
472    EmitBlock(ExitBlock);
473  }
474
475  FinishFunction();
476}
477
478void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
479                  const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
480                                                &DtorsAndObjects) {
481  StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
482                getTypes().arrangeNullaryFunction(),
483                FunctionArgList(), SourceLocation());
484
485  // Emit the dtors, in reverse order from construction.
486  for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
487    llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
488    llvm::CallInst *CI = Builder.CreateCall(Callee,
489                                            DtorsAndObjects[e - i - 1].second);
490    // Make sure the call and the callee agree on calling convention.
491    if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
492      CI->setCallingConv(F->getCallingConv());
493  }
494
495  FinishFunction();
496}
497
498/// generateDestroyHelper - Generates a helper function which, when
499/// invoked, destroys the given object.
500llvm::Function *CodeGenFunction::generateDestroyHelper(
501    llvm::Constant *addr, QualType type, Destroyer *destroyer,
502    bool useEHCleanupForArray, const VarDecl *VD) {
503  FunctionArgList args;
504  ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
505  args.push_back(&dst);
506
507  const CGFunctionInfo &FI =
508    CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args,
509                                              FunctionType::ExtInfo(),
510                                              /*variadic*/ false);
511  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
512  llvm::Function *fn =
513    CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
514
515  StartFunction(VD, getContext().VoidTy, fn, FI, args, SourceLocation());
516
517  emitDestroy(addr, type, destroyer, useEHCleanupForArray);
518
519  FinishFunction();
520
521  return fn;
522}
523