Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 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 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 18 | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, |
| 19 | llvm::Constant *DeclPtr) { |
| 20 | assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); |
| 21 | assert(!D.getType()->isReferenceType() && |
| 22 | "Should not call EmitDeclInit on a reference!"); |
| 23 | |
| 24 | CodeGenModule &CGM = CGF.CGM; |
| 25 | ASTContext &Context = CGF.getContext(); |
| 26 | |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 27 | const Expr *Init = D.getInit(); |
| 28 | QualType T = D.getType(); |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 29 | bool isVolatile = Context.getCanonicalType(T).isVolatileQualified(); |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 31 | if (!CGF.hasAggregateLLVMType(T)) { |
| 32 | llvm::Value *V = CGF.EmitScalarExpr(Init); |
| 33 | CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, T); |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 34 | } else if (T->isAnyComplexType()) { |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 35 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 36 | } else { |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 37 | CGF.EmitAggExpr(Init, DeclPtr, isVolatile); |
| 38 | |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 39 | // Avoid generating destructor(s) for initialized objects. |
| 40 | if (!isa<CXXConstructExpr>(Init)) |
| 41 | return; |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 43 | const ConstantArrayType *Array = Context.getAsConstantArrayType(T); |
| 44 | if (Array) |
| 45 | T = Context.getBaseElementType(Array); |
| 46 | |
| 47 | const RecordType *RT = T->getAs<RecordType>(); |
| 48 | if (!RT) |
| 49 | return; |
| 50 | |
| 51 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 52 | if (RD->hasTrivialDestructor()) |
| 53 | return; |
| 54 | |
| 55 | CXXDestructorDecl *Dtor = RD->getDestructor(Context); |
| 56 | |
| 57 | llvm::Constant *DtorFn; |
| 58 | if (Array) { |
| 59 | DtorFn = |
| 60 | CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, |
| 61 | Array, |
| 62 | DeclPtr); |
| 63 | const llvm::Type *Int8PtrTy = |
| 64 | llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 65 | DeclPtr = llvm::Constant::getNullValue(Int8PtrTy); |
| 66 | } else |
| 67 | DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
| 68 | |
| 69 | CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr); |
Anders Carlsson | bc49cfe | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 73 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
| 74 | llvm::Constant *DeclPtr) { |
| 75 | |
| 76 | const Expr *Init = D.getInit(); |
| 77 | QualType T = D.getType(); |
| 78 | |
| 79 | if (!T->isReferenceType()) { |
| 80 | EmitDeclInit(*this, D, DeclPtr); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | ErrorUnsupported(Init, "global variable that binds to a reference"); |
| 85 | } |
Anders Carlsson | 633c6f6 | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 86 | |
| 87 | void |
| 88 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
| 89 | llvm::Constant *DeclPtr) { |
| 90 | const llvm::Type *Int8PtrTy = |
| 91 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 92 | |
| 93 | std::vector<const llvm::Type *> Params; |
| 94 | Params.push_back(Int8PtrTy); |
| 95 | |
| 96 | // Get the destructor function type |
| 97 | const llvm::Type *DtorFnTy = |
| 98 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 99 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 100 | |
| 101 | Params.clear(); |
| 102 | Params.push_back(DtorFnTy); |
| 103 | Params.push_back(Int8PtrTy); |
| 104 | Params.push_back(Int8PtrTy); |
| 105 | |
| 106 | // Get the __cxa_atexit function type |
| 107 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 108 | const llvm::FunctionType *AtExitFnTy = |
| 109 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 110 | |
| 111 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 112 | "__cxa_atexit"); |
| 113 | |
| 114 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 115 | "__dso_handle"); |
| 116 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 117 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 118 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 119 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | CodeGenModule::EmitCXXGlobalInitFunc() { |
| 124 | if (CXXGlobalInits.empty()) |
| 125 | return; |
| 126 | |
| 127 | const llvm::FunctionType *FTy |
| 128 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 129 | false); |
| 130 | |
| 131 | // Create our global initialization function. |
| 132 | // FIXME: Should this be tweakable by targets? |
| 133 | llvm::Function *Fn = |
| 134 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 135 | "__cxx_global_initialization", &TheModule); |
| 136 | |
| 137 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 138 | &CXXGlobalInits[0], |
| 139 | CXXGlobalInits.size()); |
| 140 | AddGlobalCtor(Fn); |
| 141 | } |
| 142 | |
| 143 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 144 | const VarDecl **Decls, |
| 145 | unsigned NumDecls) { |
| 146 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 147 | SourceLocation()); |
| 148 | |
| 149 | for (unsigned i = 0; i != NumDecls; ++i) { |
| 150 | const VarDecl *D = Decls[i]; |
| 151 | |
| 152 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 153 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
| 154 | } |
| 155 | FinishFunction(); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 160 | llvm::GlobalVariable *GV) { |
| 161 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 162 | |
| 163 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 164 | "thread safe statics are currently not supported!"); |
| 165 | |
| 166 | llvm::SmallString<256> GuardVName; |
| 167 | CGM.getMangleContext().mangleGuardVariable(&D, GuardVName); |
| 168 | |
| 169 | // Create the guard variable. |
| 170 | llvm::GlobalValue *GuardV = |
| 171 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), |
| 172 | false, GV->getLinkage(), |
| 173 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)), |
| 174 | GuardVName.str()); |
| 175 | |
| 176 | // Load the first byte of the guard variable. |
| 177 | const llvm::Type *PtrTy |
| 178 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
| 179 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
| 180 | "tmp"); |
| 181 | |
| 182 | // Compare it against 0. |
| 183 | llvm::Value *nullValue |
| 184 | = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)); |
| 185 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
| 186 | |
| 187 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 188 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
| 189 | |
| 190 | // If the guard variable is 0, jump to the initializer code. |
| 191 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
| 192 | |
| 193 | EmitBlock(InitBlock); |
| 194 | |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 195 | if (D.getType()->isReferenceType()) { |
Anders Carlsson | fefc7b8 | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 196 | QualType T = D.getType(); |
Anders Carlsson | a72ddd4 | 2009-12-10 01:05:11 +0000 | [diff] [blame] | 197 | // We don't want to pass true for IsInitializer here, because a static |
| 198 | // reference to a temporary does not extend its lifetime. |
Anders Carlsson | fefc7b8 | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 199 | RValue RV = EmitReferenceBindingToExpr(D.getInit(), T, |
| 200 | /*IsInitializer=*/false); |
| 201 | EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T); |
| 202 | |
Anders Carlsson | 364051c | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 203 | } else |
| 204 | EmitDeclInit(*this, D, GV); |
Anders Carlsson | 633c6f6 | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 205 | |
| 206 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), |
| 207 | 1), |
| 208 | Builder.CreateBitCast(GuardV, PtrTy)); |
| 209 | |
| 210 | EmitBlock(EndBlock); |
| 211 | } |