Anders Carlsson | 5ec2e7c | 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 | fcbfdc1 | 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 | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 27 | const Expr *Init = D.getInit(); |
| 28 | QualType T = D.getType(); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 29 | bool isVolatile = Context.getCanonicalType(T).isVolatileQualified(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | fcbfdc1 | 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 | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 34 | } else if (T->isAnyComplexType()) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 35 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 36 | } else { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 37 | CGF.EmitAggExpr(Init, DeclPtr, isVolatile); |
| 38 | |
Anders Carlsson | 5ec2e7c | 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 | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | fcbfdc1 | 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 | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Anders Carlsson | fcbfdc1 | 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 | } |
Fariborz Jahanian | 616c173 | 2010-01-25 21:40:39 +0000 | [diff] [blame] | 83 | if (Init->isLvalue(getContext()) == Expr::LV_Valid) { |
| 84 | RValue RV = EmitReferenceBindingToExpr(Init, T, /*IsInitializer=*/true); |
| 85 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T); |
| 86 | return; |
| 87 | } |
| 88 | ErrorUnsupported(Init, |
| 89 | "global variable that binds reference to a non-lvalue"); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 90 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 91 | |
| 92 | void |
| 93 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
| 94 | llvm::Constant *DeclPtr) { |
| 95 | const llvm::Type *Int8PtrTy = |
| 96 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 97 | |
| 98 | std::vector<const llvm::Type *> Params; |
| 99 | Params.push_back(Int8PtrTy); |
| 100 | |
| 101 | // Get the destructor function type |
| 102 | const llvm::Type *DtorFnTy = |
| 103 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 104 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 105 | |
| 106 | Params.clear(); |
| 107 | Params.push_back(DtorFnTy); |
| 108 | Params.push_back(Int8PtrTy); |
| 109 | Params.push_back(Int8PtrTy); |
| 110 | |
| 111 | // Get the __cxa_atexit function type |
| 112 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 113 | const llvm::FunctionType *AtExitFnTy = |
| 114 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 115 | |
| 116 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 117 | "__cxa_atexit"); |
| 118 | |
| 119 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 120 | "__dso_handle"); |
| 121 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 122 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 123 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 124 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 125 | } |
| 126 | |
| 127 | void |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 128 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) { |
| 129 | const llvm::FunctionType *FTy |
| 130 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 131 | false); |
| 132 | |
| 133 | // Create a variable initialization function. |
| 134 | llvm::Function *Fn = |
| 135 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 136 | "__cxx_global_var_init", &TheModule); |
| 137 | |
| 138 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D); |
| 139 | |
| 140 | CXXGlobalInits.push_back(Fn); |
| 141 | } |
| 142 | |
| 143 | void |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 144 | CodeGenModule::EmitCXXGlobalInitFunc() { |
| 145 | if (CXXGlobalInits.empty()) |
| 146 | return; |
| 147 | |
| 148 | const llvm::FunctionType *FTy |
| 149 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 150 | false); |
| 151 | |
| 152 | // Create our global initialization function. |
| 153 | // FIXME: Should this be tweakable by targets? |
| 154 | llvm::Function *Fn = |
| 155 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 156 | "__cxx_global_initialization", &TheModule); |
| 157 | |
| 158 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 159 | &CXXGlobalInits[0], |
| 160 | CXXGlobalInits.size()); |
| 161 | AddGlobalCtor(Fn); |
| 162 | } |
| 163 | |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 164 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 165 | const VarDecl *D) { |
| 166 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 167 | SourceLocation()); |
| 168 | |
| 169 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 170 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
| 171 | |
| 172 | FinishFunction(); |
| 173 | } |
| 174 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 175 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 176 | llvm::Constant **Decls, |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 177 | unsigned NumDecls) { |
| 178 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 179 | SourceLocation()); |
| 180 | |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 181 | for (unsigned i = 0; i != NumDecls; ++i) |
| 182 | Builder.CreateCall(Decls[i]); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 183 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 184 | FinishFunction(); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 189 | llvm::GlobalVariable *GV) { |
| 190 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 191 | |
| 192 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 193 | "thread safe statics are currently not supported!"); |
| 194 | |
| 195 | llvm::SmallString<256> GuardVName; |
| 196 | CGM.getMangleContext().mangleGuardVariable(&D, GuardVName); |
| 197 | |
| 198 | // Create the guard variable. |
| 199 | llvm::GlobalValue *GuardV = |
| 200 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), |
| 201 | false, GV->getLinkage(), |
| 202 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)), |
| 203 | GuardVName.str()); |
| 204 | |
| 205 | // Load the first byte of the guard variable. |
| 206 | const llvm::Type *PtrTy |
| 207 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
| 208 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
| 209 | "tmp"); |
| 210 | |
| 211 | // Compare it against 0. |
| 212 | llvm::Value *nullValue |
| 213 | = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)); |
| 214 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
| 215 | |
| 216 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 217 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
| 218 | |
| 219 | // If the guard variable is 0, jump to the initializer code. |
| 220 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
| 221 | |
| 222 | EmitBlock(InitBlock); |
| 223 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 224 | if (D.getType()->isReferenceType()) { |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 225 | QualType T = D.getType(); |
Anders Carlsson | c7974ca | 2009-12-10 01:05:11 +0000 | [diff] [blame] | 226 | // We don't want to pass true for IsInitializer here, because a static |
| 227 | // reference to a temporary does not extend its lifetime. |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 228 | RValue RV = EmitReferenceBindingToExpr(D.getInit(), T, |
| 229 | /*IsInitializer=*/false); |
| 230 | EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T); |
| 231 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 232 | } else |
| 233 | EmitDeclInit(*this, D, GV); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 234 | |
| 235 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), |
| 236 | 1), |
| 237 | Builder.CreateBitCast(GuardV, PtrTy)); |
| 238 | |
| 239 | EmitBlock(EndBlock); |
| 240 | } |