1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 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 coordinates the per-module state used while generating code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenModule.h" 15 #include "CGDebugInfo.h" 16 #include "CodeGenFunction.h" 17 #include "CGCall.h" 18 #include "CGObjCRuntime.h" 19 #include "Mangle.h" 20 #include "TargetInfo.h" 21 #include "clang/CodeGen/CodeGenOptions.h" 22 #include "clang/AST/ASTContext.h" 23 #include "clang/AST/CharUnits.h" 24 #include "clang/AST/DeclObjC.h" 25 #include "clang/AST/DeclCXX.h" 26 #include "clang/AST/RecordLayout.h" 27 #include "clang/Basic/Builtins.h" 28 #include "clang/Basic/Diagnostic.h" 29 #include "clang/Basic/SourceManager.h" 30 #include "clang/Basic/TargetInfo.h" 31 #include "clang/Basic/ConvertUTF.h" 32 #include "llvm/CallingConv.h" 33 #include "llvm/Module.h" 34 #include "llvm/Intrinsics.h" 35 #include "llvm/LLVMContext.h" 36 #include "llvm/ADT/Triple.h" 37 #include "llvm/Target/TargetData.h" 38 #include "llvm/Support/CallSite.h" 39 #include "llvm/Support/ErrorHandling.h" 40 using namespace clang; 41 using namespace CodeGen; 42 43 44 CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, 45 llvm::Module &M, const llvm::TargetData &TD, 46 Diagnostic &diags) 47 : BlockModule(C, M, TD, Types, *this), Context(C), 48 Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), 49 TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), 50 Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()), 51 VTables(*this), Runtime(0), ABI(0), 52 CFConstantStringClassRef(0), 53 NSConstantStringClassRef(0), 54 VMContext(M.getContext()) { 55 56 if (!Features.ObjC1) 57 Runtime = 0; 58 else if (!Features.NeXTRuntime) 59 Runtime = CreateGNUObjCRuntime(*this); 60 else if (Features.ObjCNonFragileABI) 61 Runtime = CreateMacNonFragileABIObjCRuntime(*this); 62 else 63 Runtime = CreateMacObjCRuntime(*this); 64 65 if (!Features.CPlusPlus) 66 ABI = 0; 67 else createCXXABI(); 68 69 // If debug info generation is enabled, create the CGDebugInfo object. 70 DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0; 71 } 72 73 CodeGenModule::~CodeGenModule() { 74 delete Runtime; 75 delete ABI; 76 delete DebugInfo; 77 } 78 79 void CodeGenModule::createObjCRuntime() { 80 if (!Features.NeXTRuntime) 81 Runtime = CreateGNUObjCRuntime(*this); 82 else if (Features.ObjCNonFragileABI) 83 Runtime = CreateMacNonFragileABIObjCRuntime(*this); 84 else 85 Runtime = CreateMacObjCRuntime(*this); 86 } 87 88 void CodeGenModule::createCXXABI() { 89 // For now, just create an Itanium ABI. 90 ABI = CreateItaniumCXXABI(*this); 91 } 92 93 void CodeGenModule::Release() { 94 EmitDeferred(); 95 EmitCXXGlobalInitFunc(); 96 EmitCXXGlobalDtorFunc(); 97 if (Runtime) 98 if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 99 AddGlobalCtor(ObjCInitFunction); 100 EmitCtorList(GlobalCtors, "llvm.global_ctors"); 101 EmitCtorList(GlobalDtors, "llvm.global_dtors"); 102 EmitAnnotations(); 103 EmitLLVMUsed(); 104 } 105 106 bool CodeGenModule::isTargetDarwin() const { 107 return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin; 108 } 109 110 /// ErrorUnsupported - Print out an error that codegen doesn't support the 111 /// specified stmt yet. 112 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 113 bool OmitOnError) { 114 if (OmitOnError && getDiags().hasErrorOccurred()) 115 return; 116 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 117 "cannot compile this %0 yet"); 118 std::string Msg = Type; 119 getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 120 << Msg << S->getSourceRange(); 121 } 122 123 /// ErrorUnsupported - Print out an error that codegen doesn't support the 124 /// specified decl yet. 125 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 126 bool OmitOnError) { 127 if (OmitOnError && getDiags().hasErrorOccurred()) 128 return; 129 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 130 "cannot compile this %0 yet"); 131 std::string Msg = Type; 132 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 133 } 134 135 LangOptions::VisibilityMode 136 CodeGenModule::getDeclVisibilityMode(const Decl *D) const { 137 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 138 if (VD->getStorageClass() == VarDecl::PrivateExtern) 139 return LangOptions::Hidden; 140 141 if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { 142 switch (attr->getVisibility()) { 143 default: assert(0 && "Unknown visibility!"); 144 case VisibilityAttr::DefaultVisibility: 145 return LangOptions::Default; 146 case VisibilityAttr::HiddenVisibility: 147 return LangOptions::Hidden; 148 case VisibilityAttr::ProtectedVisibility: 149 return LangOptions::Protected; 150 } 151 } 152 153 // This decl should have the same visibility as its parent. 154 if (const DeclContext *DC = D->getDeclContext()) 155 return getDeclVisibilityMode(cast<Decl>(DC)); 156 157 return getLangOptions().getVisibilityMode(); 158 } 159 160 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 161 const Decl *D) const { 162 // Internal definitions always have default visibility. 163 if (GV->hasLocalLinkage()) { 164 GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 165 return; 166 } 167 168 switch (getDeclVisibilityMode(D)) { 169 default: assert(0 && "Unknown visibility!"); 170 case LangOptions::Default: 171 return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 172 case LangOptions::Hidden: 173 return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 174 case LangOptions::Protected: 175 return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 176 } 177 } 178 179 void CodeGenModule::getMangledName(MangleBuffer &Buffer, GlobalDecl GD) { 180 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); 181 182 if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 183 return getMangledCXXCtorName(Buffer, D, GD.getCtorType()); 184 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 185 return getMangledCXXDtorName(Buffer, D, GD.getDtorType()); 186 187 if (!getMangleContext().shouldMangleDeclName(ND)) { 188 assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 189 Buffer.setString(ND->getNameAsCString()); 190 return; 191 } 192 193 getMangleContext().mangleName(ND, Buffer.getBuffer()); 194 } 195 196 llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) { 197 return getModule().getNamedValue(Name); 198 } 199 200 /// AddGlobalCtor - Add a function to the list that will be called before 201 /// main() runs. 202 void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 203 // FIXME: Type coercion of void()* types. 204 GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 205 } 206 207 /// AddGlobalDtor - Add a function to the list that will be called 208 /// when the module is unloaded. 209 void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 210 // FIXME: Type coercion of void()* types. 211 GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 212 } 213 214 void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 215 // Ctor function type is void()*. 216 llvm::FunctionType* CtorFTy = 217 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 218 std::vector<const llvm::Type*>(), 219 false); 220 llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 221 222 // Get the type of a ctor entry, { i32, void ()* }. 223 llvm::StructType* CtorStructTy = 224 llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext), 225 llvm::PointerType::getUnqual(CtorFTy), NULL); 226 227 // Construct the constructor and destructor arrays. 228 std::vector<llvm::Constant*> Ctors; 229 for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 230 std::vector<llvm::Constant*> S; 231 S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 232 I->second, false)); 233 S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 234 Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 235 } 236 237 if (!Ctors.empty()) { 238 llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 239 new llvm::GlobalVariable(TheModule, AT, false, 240 llvm::GlobalValue::AppendingLinkage, 241 llvm::ConstantArray::get(AT, Ctors), 242 GlobalName); 243 } 244 } 245 246 void CodeGenModule::EmitAnnotations() { 247 if (Annotations.empty()) 248 return; 249 250 // Create a new global variable for the ConstantStruct in the Module. 251 llvm::Constant *Array = 252 llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 253 Annotations.size()), 254 Annotations); 255 llvm::GlobalValue *gv = 256 new llvm::GlobalVariable(TheModule, Array->getType(), false, 257 llvm::GlobalValue::AppendingLinkage, Array, 258 "llvm.global.annotations"); 259 gv->setSection("llvm.metadata"); 260 } 261 262 static CodeGenModule::GVALinkage 263 GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD, 264 const LangOptions &Features) { 265 CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal; 266 267 Linkage L = FD->getLinkage(); 268 if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus && 269 FD->getType()->getLinkage() == UniqueExternalLinkage) 270 L = UniqueExternalLinkage; 271 272 switch (L) { 273 case NoLinkage: 274 case InternalLinkage: 275 case UniqueExternalLinkage: 276 return CodeGenModule::GVA_Internal; 277 278 case ExternalLinkage: 279 switch (FD->getTemplateSpecializationKind()) { 280 case TSK_Undeclared: 281 case TSK_ExplicitSpecialization: 282 External = CodeGenModule::GVA_StrongExternal; 283 break; 284 285 case TSK_ExplicitInstantiationDefinition: 286 return CodeGenModule::GVA_ExplicitTemplateInstantiation; 287 288 case TSK_ExplicitInstantiationDeclaration: 289 case TSK_ImplicitInstantiation: 290 External = CodeGenModule::GVA_TemplateInstantiation; 291 break; 292 } 293 } 294 295 if (!FD->isInlined()) 296 return External; 297 298 if (!Features.CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { 299 // GNU or C99 inline semantics. Determine whether this symbol should be 300 // externally visible. 301 if (FD->isInlineDefinitionExternallyVisible()) 302 return External; 303 304 // C99 inline semantics, where the symbol is not externally visible. 305 return CodeGenModule::GVA_C99Inline; 306 } 307 308 // C++0x [temp.explicit]p9: 309 // [ Note: The intent is that an inline function that is the subject of 310 // an explicit instantiation declaration will still be implicitly 311 // instantiated when used so that the body can be considered for 312 // inlining, but that no out-of-line copy of the inline function would be 313 // generated in the translation unit. -- end note ] 314 if (FD->getTemplateSpecializationKind() 315 == TSK_ExplicitInstantiationDeclaration) 316 return CodeGenModule::GVA_C99Inline; 317 318 return CodeGenModule::GVA_CXXInline; 319 } 320 321 llvm::GlobalValue::LinkageTypes 322 CodeGenModule::getFunctionLinkage(const FunctionDecl *D) { 323 GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features); 324 325 if (Linkage == GVA_Internal) { 326 return llvm::Function::InternalLinkage; 327 } else if (D->hasAttr<DLLExportAttr>()) { 328 return llvm::Function::DLLExportLinkage; 329 } else if (D->hasAttr<WeakAttr>()) { 330 return llvm::Function::WeakAnyLinkage; 331 } else if (Linkage == GVA_C99Inline) { 332 // In C99 mode, 'inline' functions are guaranteed to have a strong 333 // definition somewhere else, so we can use available_externally linkage. 334 return llvm::Function::AvailableExternallyLinkage; 335 } else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) { 336 // In C++, the compiler has to emit a definition in every translation unit 337 // that references the function. We should use linkonce_odr because 338 // a) if all references in this translation unit are optimized away, we 339 // don't need to codegen it. b) if the function persists, it needs to be 340 // merged with other definitions. c) C++ has the ODR, so we know the 341 // definition is dependable. 342 return llvm::Function::LinkOnceODRLinkage; 343 } else if (Linkage == GVA_ExplicitTemplateInstantiation) { 344 // An explicit instantiation of a template has weak linkage, since 345 // explicit instantiations can occur in multiple translation units 346 // and must all be equivalent. However, we are not allowed to 347 // throw away these explicit instantiations. 348 return llvm::Function::WeakODRLinkage; 349 } else { 350 assert(Linkage == GVA_StrongExternal); 351 // Otherwise, we have strong external linkage. 352 return llvm::Function::ExternalLinkage; 353 } 354 } 355 356 357 /// SetFunctionDefinitionAttributes - Set attributes for a global. 358 /// 359 /// FIXME: This is currently only done for aliases and functions, but not for 360 /// variables (these details are set in EmitGlobalVarDefinition for variables). 361 void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 362 llvm::GlobalValue *GV) { 363 SetCommonAttributes(D, GV); 364 } 365 366 void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 367 const CGFunctionInfo &Info, 368 llvm::Function *F) { 369 unsigned CallingConv; 370 AttributeListType AttributeList; 371 ConstructAttributeList(Info, D, AttributeList, CallingConv); 372 F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 373 AttributeList.size())); 374 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 375 } 376 377 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 378 llvm::Function *F) { 379 if (!Features.Exceptions && !Features.ObjCNonFragileABI) 380 F->addFnAttr(llvm::Attribute::NoUnwind); 381 382 if (D->hasAttr<AlwaysInlineAttr>()) 383 F->addFnAttr(llvm::Attribute::AlwaysInline); 384 385 if (D->hasAttr<NoInlineAttr>()) 386 F->addFnAttr(llvm::Attribute::NoInline); 387 388 if (Features.getStackProtectorMode() == LangOptions::SSPOn) 389 F->addFnAttr(llvm::Attribute::StackProtect); 390 else if (Features.getStackProtectorMode() == LangOptions::SSPReq) 391 F->addFnAttr(llvm::Attribute::StackProtectReq); 392 393 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) { 394 unsigned width = Context.Target.getCharWidth(); 395 F->setAlignment(AA->getAlignment() / width); 396 while ((AA = AA->getNext<AlignedAttr>())) 397 F->setAlignment(std::max(F->getAlignment(), AA->getAlignment() / width)); 398 } 399 // C++ ABI requires 2-byte alignment for member functions. 400 if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 401 F->setAlignment(2); 402 } 403 404 void CodeGenModule::SetCommonAttributes(const Decl *D, 405 llvm::GlobalValue *GV) { 406 setGlobalVisibility(GV, D); 407 408 if (D->hasAttr<UsedAttr>()) 409 AddUsedGlobal(GV); 410 411 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 412 GV->setSection(SA->getName()); 413 414 getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); 415 } 416 417 void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 418 llvm::Function *F, 419 const CGFunctionInfo &FI) { 420 SetLLVMFunctionAttributes(D, FI, F); 421 SetLLVMFunctionAttributesForDefinition(D, F); 422 423 F->setLinkage(llvm::Function::InternalLinkage); 424 425 SetCommonAttributes(D, F); 426 } 427 428 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, 429 llvm::Function *F, 430 bool IsIncompleteFunction) { 431 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 432 433 if (!IsIncompleteFunction) 434 SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); 435 436 // Only a few attributes are set on declarations; these may later be 437 // overridden by a definition. 438 439 if (FD->hasAttr<DLLImportAttr>()) { 440 F->setLinkage(llvm::Function::DLLImportLinkage); 441 } else if (FD->hasAttr<WeakAttr>() || 442 FD->hasAttr<WeakImportAttr>()) { 443 // "extern_weak" is overloaded in LLVM; we probably should have 444 // separate linkage types for this. 445 F->setLinkage(llvm::Function::ExternalWeakLinkage); 446 } else { 447 F->setLinkage(llvm::Function::ExternalLinkage); 448 } 449 450 if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 451 F->setSection(SA->getName()); 452 } 453 454 void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 455 assert(!GV->isDeclaration() && 456 "Only globals with definition can force usage."); 457 LLVMUsed.push_back(GV); 458 } 459 460 void CodeGenModule::EmitLLVMUsed() { 461 // Don't create llvm.used if there is no need. 462 if (LLVMUsed.empty()) 463 return; 464 465 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 466 467 // Convert LLVMUsed to what ConstantArray needs. 468 std::vector<llvm::Constant*> UsedArray; 469 UsedArray.resize(LLVMUsed.size()); 470 for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 471 UsedArray[i] = 472 llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 473 i8PTy); 474 } 475 476 if (UsedArray.empty()) 477 return; 478 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 479 480 llvm::GlobalVariable *GV = 481 new llvm::GlobalVariable(getModule(), ATy, false, 482 llvm::GlobalValue::AppendingLinkage, 483 llvm::ConstantArray::get(ATy, UsedArray), 484 "llvm.used"); 485 486 GV->setSection("llvm.metadata"); 487 } 488 489 void CodeGenModule::EmitDeferred() { 490 // Emit code for any potentially referenced deferred decls. Since a 491 // previously unused static decl may become used during the generation of code 492 // for a static function, iterate until no changes are made. 493 494 while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) { 495 if (!DeferredVTables.empty()) { 496 const CXXRecordDecl *RD = DeferredVTables.back(); 497 DeferredVTables.pop_back(); 498 getVTables().GenerateClassData(getVTableLinkage(RD), RD); 499 continue; 500 } 501 502 GlobalDecl D = DeferredDeclsToEmit.back(); 503 DeferredDeclsToEmit.pop_back(); 504 505 // Check to see if we've already emitted this. This is necessary 506 // for a couple of reasons: first, decls can end up in the 507 // deferred-decls queue multiple times, and second, decls can end 508 // up with definitions in unusual ways (e.g. by an extern inline 509 // function acquiring a strong function redefinition). Just 510 // ignore these cases. 511 // 512 // TODO: That said, looking this up multiple times is very wasteful. 513 MangleBuffer Name; 514 getMangledName(Name, D); 515 llvm::GlobalValue *CGRef = GetGlobalValue(Name); 516 assert(CGRef && "Deferred decl wasn't referenced?"); 517 518 if (!CGRef->isDeclaration()) 519 continue; 520 521 // GlobalAlias::isDeclaration() defers to the aliasee, but for our 522 // purposes an alias counts as a definition. 523 if (isa<llvm::GlobalAlias>(CGRef)) 524 continue; 525 526 // Otherwise, emit the definition and move on to the next one. 527 EmitGlobalDefinition(D); 528 } 529 } 530 531 /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 532 /// annotation information for a given GlobalValue. The annotation struct is 533 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 534 /// GlobalValue being annotated. The second field is the constant string 535 /// created from the AnnotateAttr's annotation. The third field is a constant 536 /// string containing the name of the translation unit. The fourth field is 537 /// the line number in the file of the annotated value declaration. 538 /// 539 /// FIXME: this does not unique the annotation string constants, as llvm-gcc 540 /// appears to. 541 /// 542 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 543 const AnnotateAttr *AA, 544 unsigned LineNo) { 545 llvm::Module *M = &getModule(); 546 547 // get [N x i8] constants for the annotation string, and the filename string 548 // which are the 2nd and 3rd elements of the global annotation structure. 549 const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); 550 llvm::Constant *anno = llvm::ConstantArray::get(VMContext, 551 AA->getAnnotation(), true); 552 llvm::Constant *unit = llvm::ConstantArray::get(VMContext, 553 M->getModuleIdentifier(), 554 true); 555 556 // Get the two global values corresponding to the ConstantArrays we just 557 // created to hold the bytes of the strings. 558 llvm::GlobalValue *annoGV = 559 new llvm::GlobalVariable(*M, anno->getType(), false, 560 llvm::GlobalValue::PrivateLinkage, anno, 561 GV->getName()); 562 // translation unit name string, emitted into the llvm.metadata section. 563 llvm::GlobalValue *unitGV = 564 new llvm::GlobalVariable(*M, unit->getType(), false, 565 llvm::GlobalValue::PrivateLinkage, unit, 566 ".str"); 567 568 // Create the ConstantStruct for the global annotation. 569 llvm::Constant *Fields[4] = { 570 llvm::ConstantExpr::getBitCast(GV, SBP), 571 llvm::ConstantExpr::getBitCast(annoGV, SBP), 572 llvm::ConstantExpr::getBitCast(unitGV, SBP), 573 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) 574 }; 575 return llvm::ConstantStruct::get(VMContext, Fields, 4, false); 576 } 577 578 bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 579 // Never defer when EmitAllDecls is specified or the decl has 580 // attribute used. 581 if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) 582 return false; 583 584 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 585 // Constructors and destructors should never be deferred. 586 if (FD->hasAttr<ConstructorAttr>() || 587 FD->hasAttr<DestructorAttr>()) 588 return false; 589 590 // The key function for a class must never be deferred. 591 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Global)) { 592 const CXXRecordDecl *RD = MD->getParent(); 593 if (MD->isOutOfLine() && RD->isDynamicClass()) { 594 const CXXMethodDecl *KeyFunction = getContext().getKeyFunction(RD); 595 if (KeyFunction && 596 KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) 597 return false; 598 } 599 } 600 601 GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features); 602 603 // static, static inline, always_inline, and extern inline functions can 604 // always be deferred. Normal inline functions can be deferred in C99/C++. 605 // Implicit template instantiations can also be deferred in C++. 606 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || 607 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) 608 return true; 609 return false; 610 } 611 612 const VarDecl *VD = cast<VarDecl>(Global); 613 assert(VD->isFileVarDecl() && "Invalid decl"); 614 615 // We never want to defer structs that have non-trivial constructors or 616 // destructors. 617 618 // FIXME: Handle references. 619 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { 620 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 621 if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()) 622 return false; 623 } 624 } 625 626 // Static data may be deferred, but out-of-line static data members 627 // cannot be. 628 Linkage L = VD->getLinkage(); 629 if (L == ExternalLinkage && getContext().getLangOptions().CPlusPlus && 630 VD->getType()->getLinkage() == UniqueExternalLinkage) 631 L = UniqueExternalLinkage; 632 633 switch (L) { 634 case NoLinkage: 635 case InternalLinkage: 636 case UniqueExternalLinkage: 637 // Initializer has side effects? 638 if (VD->getInit() && VD->getInit()->HasSideEffects(Context)) 639 return false; 640 return !(VD->isStaticDataMember() && VD->isOutOfLine()); 641 642 case ExternalLinkage: 643 break; 644 } 645 646 return false; 647 } 648 649 llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { 650 const AliasAttr *AA = VD->getAttr<AliasAttr>(); 651 assert(AA && "No alias?"); 652 653 const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); 654 655 // See if there is already something with the target's name in the module. 656 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); 657 658 llvm::Constant *Aliasee; 659 if (isa<llvm::FunctionType>(DeclTy)) 660 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl()); 661 else 662 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 663 llvm::PointerType::getUnqual(DeclTy), 0); 664 if (!Entry) { 665 llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee); 666 F->setLinkage(llvm::Function::ExternalWeakLinkage); 667 WeakRefReferences.insert(F); 668 } 669 670 return Aliasee; 671 } 672 673 void CodeGenModule::EmitGlobal(GlobalDecl GD) { 674 const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); 675 676 // Weak references don't produce any output by themselves. 677 if (Global->hasAttr<WeakRefAttr>()) 678 return; 679 680 // If this is an alias definition (which otherwise looks like a declaration) 681 // emit it now. 682 if (Global->hasAttr<AliasAttr>()) 683 return EmitAliasDefinition(GD); 684 685 // Ignore declarations, they will be emitted on their first use. 686 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 687 // Forward declarations are emitted lazily on first use. 688 if (!FD->isThisDeclarationADefinition()) 689 return; 690 } else { 691 const VarDecl *VD = cast<VarDecl>(Global); 692 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 693 694 if (VD->isThisDeclarationADefinition() != VarDecl::Definition) 695 return; 696 } 697 698 // Defer code generation when possible if this is a static definition, inline 699 // function etc. These we only want to emit if they are used. 700 if (!MayDeferGeneration(Global)) { 701 // Emit the definition if it can't be deferred. 702 EmitGlobalDefinition(GD); 703 return; 704 } 705 706 // If the value has already been used, add it directly to the 707 // DeferredDeclsToEmit list. 708 MangleBuffer MangledName; 709 getMangledName(MangledName, GD); 710 if (GetGlobalValue(MangledName)) 711 DeferredDeclsToEmit.push_back(GD); 712 else { 713 // Otherwise, remember that we saw a deferred decl with this name. The 714 // first use of the mangled name will cause it to move into 715 // DeferredDeclsToEmit. 716 DeferredDecls[MangledName] = GD; 717 } 718 } 719 720 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 721 const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 722 723 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), 724 Context.getSourceManager(), 725 "Generating code for declaration"); 726 727 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) 728 if (Method->isVirtual()) 729 getVTables().EmitThunks(GD); 730 731 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 732 return EmitCXXConstructor(CD, GD.getCtorType()); 733 734 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 735 return EmitCXXDestructor(DD, GD.getDtorType()); 736 737 if (isa<FunctionDecl>(D)) 738 return EmitGlobalFunctionDefinition(GD); 739 740 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 741 return EmitGlobalVarDefinition(VD); 742 743 assert(0 && "Invalid argument to EmitGlobalDefinition()"); 744 } 745 746 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the 747 /// module, create and return an llvm Function with the specified type. If there 748 /// is something in the module with the specified name, return it potentially 749 /// bitcasted to the right type. 750 /// 751 /// If D is non-null, it specifies a decl that correspond to this. This is used 752 /// to set the attributes on the function when it is first created. 753 llvm::Constant * 754 CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName, 755 const llvm::Type *Ty, 756 GlobalDecl D) { 757 // Lookup the entry, lazily creating it if necessary. 758 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 759 if (Entry) { 760 if (WeakRefReferences.count(Entry)) { 761 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl()); 762 if (FD && !FD->hasAttr<WeakAttr>()) 763 Entry->setLinkage(llvm::Function::ExternalLinkage); 764 765 WeakRefReferences.erase(Entry); 766 } 767 768 if (Entry->getType()->getElementType() == Ty) 769 return Entry; 770 771 // Make sure the result is of the correct type. 772 const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 773 return llvm::ConstantExpr::getBitCast(Entry, PTy); 774 } 775 776 // This function doesn't have a complete type (for example, the return 777 // type is an incomplete struct). Use a fake type instead, and make 778 // sure not to try to set attributes. 779 bool IsIncompleteFunction = false; 780 781 const llvm::FunctionType *FTy; 782 if (isa<llvm::FunctionType>(Ty)) { 783 FTy = cast<llvm::FunctionType>(Ty); 784 } else { 785 FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 786 std::vector<const llvm::Type*>(), false); 787 IsIncompleteFunction = true; 788 } 789 llvm::Function *F = llvm::Function::Create(FTy, 790 llvm::Function::ExternalLinkage, 791 MangledName, &getModule()); 792 assert(F->getName() == MangledName && "name was uniqued!"); 793 if (D.getDecl()) 794 SetFunctionAttributes(D, F, IsIncompleteFunction); 795 796 // This is the first use or definition of a mangled name. If there is a 797 // deferred decl with this name, remember that we need to emit it at the end 798 // of the file. 799 llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 800 if (DDI != DeferredDecls.end()) { 801 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 802 // list, and remove it from DeferredDecls (since we don't need it anymore). 803 DeferredDeclsToEmit.push_back(DDI->second); 804 DeferredDecls.erase(DDI); 805 } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) { 806 // If this the first reference to a C++ inline function in a class, queue up 807 // the deferred function body for emission. These are not seen as 808 // top-level declarations. 809 if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD)) 810 DeferredDeclsToEmit.push_back(D); 811 // A called constructor which has no definition or declaration need be 812 // synthesized. 813 else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 814 if (CD->isImplicit()) { 815 assert(CD->isUsed() && "Sema doesn't consider constructor as used."); 816 DeferredDeclsToEmit.push_back(D); 817 } 818 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { 819 if (DD->isImplicit()) { 820 assert(DD->isUsed() && "Sema doesn't consider destructor as used."); 821 DeferredDeclsToEmit.push_back(D); 822 } 823 } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 824 if (MD->isCopyAssignment() && MD->isImplicit()) { 825 assert(MD->isUsed() && "Sema doesn't consider CopyAssignment as used."); 826 DeferredDeclsToEmit.push_back(D); 827 } 828 } 829 } 830 831 // Make sure the result is of the requested type. 832 if (!IsIncompleteFunction) { 833 assert(F->getType()->getElementType() == Ty); 834 return F; 835 } 836 837 const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 838 return llvm::ConstantExpr::getBitCast(F, PTy); 839 } 840 841 /// GetAddrOfFunction - Return the address of the given function. If Ty is 842 /// non-null, then this function will use the specified type if it has to 843 /// create it (this occurs when we see a definition of the function). 844 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 845 const llvm::Type *Ty) { 846 // If there was no specific requested type, just convert it now. 847 if (!Ty) 848 Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); 849 MangleBuffer MangledName; 850 getMangledName(MangledName, GD); 851 return GetOrCreateLLVMFunction(MangledName, Ty, GD); 852 } 853 854 /// CreateRuntimeFunction - Create a new runtime function with the specified 855 /// type and name. 856 llvm::Constant * 857 CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 858 llvm::StringRef Name) { 859 return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); 860 } 861 862 static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { 863 if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) 864 return false; 865 if (Context.getLangOptions().CPlusPlus && 866 Context.getBaseElementType(D->getType())->getAs<RecordType>()) { 867 // FIXME: We should do something fancier here! 868 return false; 869 } 870 return true; 871 } 872 873 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 874 /// create and return an llvm GlobalVariable with the specified type. If there 875 /// is something in the module with the specified name, return it potentially 876 /// bitcasted to the right type. 877 /// 878 /// If D is non-null, it specifies a decl that correspond to this. This is used 879 /// to set the attributes on the global when it is first created. 880 llvm::Constant * 881 CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, 882 const llvm::PointerType *Ty, 883 const VarDecl *D) { 884 // Lookup the entry, lazily creating it if necessary. 885 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 886 if (Entry) { 887 if (WeakRefReferences.count(Entry)) { 888 if (D && !D->hasAttr<WeakAttr>()) 889 Entry->setLinkage(llvm::Function::ExternalLinkage); 890 891 WeakRefReferences.erase(Entry); 892 } 893 894 if (Entry->getType() == Ty) 895 return Entry; 896 897 // Make sure the result is of the correct type. 898 return llvm::ConstantExpr::getBitCast(Entry, Ty); 899 } 900 901 // This is the first use or definition of a mangled name. If there is a 902 // deferred decl with this name, remember that we need to emit it at the end 903 // of the file. 904 llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 905 if (DDI != DeferredDecls.end()) { 906 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 907 // list, and remove it from DeferredDecls (since we don't need it anymore). 908 DeferredDeclsToEmit.push_back(DDI->second); 909 DeferredDecls.erase(DDI); 910 } 911 912 llvm::GlobalVariable *GV = 913 new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 914 llvm::GlobalValue::ExternalLinkage, 915 0, MangledName, 0, 916 false, Ty->getAddressSpace()); 917 918 // Handle things which are present even on external declarations. 919 if (D) { 920 // FIXME: This code is overly simple and should be merged with other global 921 // handling. 922 GV->setConstant(DeclIsConstantGlobal(Context, D)); 923 924 // FIXME: Merge with other attribute handling code. 925 if (D->getStorageClass() == VarDecl::PrivateExtern) 926 GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 927 928 if (D->hasAttr<WeakAttr>() || 929 D->hasAttr<WeakImportAttr>()) 930 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 931 932 GV->setThreadLocal(D->isThreadSpecified()); 933 } 934 935 return GV; 936 } 937 938 939 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 940 /// given global variable. If Ty is non-null and if the global doesn't exist, 941 /// then it will be greated with the specified type instead of whatever the 942 /// normal requested type would be. 943 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 944 const llvm::Type *Ty) { 945 assert(D->hasGlobalStorage() && "Not a global variable"); 946 QualType ASTTy = D->getType(); 947 if (Ty == 0) 948 Ty = getTypes().ConvertTypeForMem(ASTTy); 949 950 const llvm::PointerType *PTy = 951 llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 952 953 MangleBuffer MangledName; 954 getMangledName(MangledName, D); 955 return GetOrCreateLLVMGlobal(MangledName, PTy, D); 956 } 957 958 /// CreateRuntimeVariable - Create a new runtime global variable with the 959 /// specified type and name. 960 llvm::Constant * 961 CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 962 llvm::StringRef Name) { 963 return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 964 } 965 966 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 967 assert(!D->getInit() && "Cannot emit definite definitions here!"); 968 969 if (MayDeferGeneration(D)) { 970 // If we have not seen a reference to this variable yet, place it 971 // into the deferred declarations table to be emitted if needed 972 // later. 973 MangleBuffer MangledName; 974 getMangledName(MangledName, D); 975 if (!GetGlobalValue(MangledName)) { 976 DeferredDecls[MangledName] = D; 977 return; 978 } 979 } 980 981 // The tentative definition is the only definition. 982 EmitGlobalVarDefinition(D); 983 } 984 985 void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) { 986 if (DefinitionRequired) 987 getVTables().GenerateClassData(getVTableLinkage(Class), Class); 988 } 989 990 llvm::GlobalVariable::LinkageTypes 991 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { 992 if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) 993 return llvm::GlobalVariable::InternalLinkage; 994 995 if (const CXXMethodDecl *KeyFunction 996 = RD->getASTContext().getKeyFunction(RD)) { 997 // If this class has a key function, use that to determine the linkage of 998 // the vtable. 999 const FunctionDecl *Def = 0; 1000 if (KeyFunction->getBody(Def)) 1001 KeyFunction = cast<CXXMethodDecl>(Def); 1002 1003 switch (KeyFunction->getTemplateSpecializationKind()) { 1004 case TSK_Undeclared: 1005 case TSK_ExplicitSpecialization: 1006 if (KeyFunction->isInlined()) 1007 return llvm::GlobalVariable::WeakODRLinkage; 1008 1009 return llvm::GlobalVariable::ExternalLinkage; 1010 1011 case TSK_ImplicitInstantiation: 1012 case TSK_ExplicitInstantiationDefinition: 1013 return llvm::GlobalVariable::WeakODRLinkage; 1014 1015 case TSK_ExplicitInstantiationDeclaration: 1016 // FIXME: Use available_externally linkage. However, this currently 1017 // breaks LLVM's build due to undefined symbols. 1018 // return llvm::GlobalVariable::AvailableExternallyLinkage; 1019 return llvm::GlobalVariable::WeakODRLinkage; 1020 } 1021 } 1022 1023 switch (RD->getTemplateSpecializationKind()) { 1024 case TSK_Undeclared: 1025 case TSK_ExplicitSpecialization: 1026 case TSK_ImplicitInstantiation: 1027 case TSK_ExplicitInstantiationDefinition: 1028 return llvm::GlobalVariable::WeakODRLinkage; 1029 1030 case TSK_ExplicitInstantiationDeclaration: 1031 // FIXME: Use available_externally linkage. However, this currently 1032 // breaks LLVM's build due to undefined symbols. 1033 // return llvm::GlobalVariable::AvailableExternallyLinkage; 1034 return llvm::GlobalVariable::WeakODRLinkage; 1035 } 1036 1037 // Silence GCC warning. 1038 return llvm::GlobalVariable::WeakODRLinkage; 1039 } 1040 1041 static CodeGenModule::GVALinkage 1042 GetLinkageForVariable(ASTContext &Context, const VarDecl *VD) { 1043 // If this is a static data member, compute the kind of template 1044 // specialization. Otherwise, this variable is not part of a 1045 // template. 1046 TemplateSpecializationKind TSK = TSK_Undeclared; 1047 if (VD->isStaticDataMember()) 1048 TSK = VD->getTemplateSpecializationKind(); 1049 1050 Linkage L = VD->getLinkage(); 1051 if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus && 1052 VD->getType()->getLinkage() == UniqueExternalLinkage) 1053 L = UniqueExternalLinkage; 1054 1055 switch (L) { 1056 case NoLinkage: 1057 case InternalLinkage: 1058 case UniqueExternalLinkage: 1059 return CodeGenModule::GVA_Internal; 1060 1061 case ExternalLinkage: 1062 switch (TSK) { 1063 case TSK_Undeclared: 1064 case TSK_ExplicitSpecialization: 1065 return CodeGenModule::GVA_StrongExternal; 1066 1067 case TSK_ExplicitInstantiationDeclaration: 1068 llvm_unreachable("Variable should not be instantiated"); 1069 // Fall through to treat this like any other instantiation. 1070 1071 case TSK_ExplicitInstantiationDefinition: 1072 return CodeGenModule::GVA_ExplicitTemplateInstantiation; 1073 1074 case TSK_ImplicitInstantiation: 1075 return CodeGenModule::GVA_TemplateInstantiation; 1076 } 1077 } 1078 1079 return CodeGenModule::GVA_StrongExternal; 1080 } 1081 1082 CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const { 1083 return CharUnits::fromQuantity( 1084 TheTargetData.getTypeStoreSizeInBits(Ty) / Context.getCharWidth()); 1085 } 1086 1087 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 1088 llvm::Constant *Init = 0; 1089 QualType ASTTy = D->getType(); 1090 bool NonConstInit = false; 1091 1092 const Expr *InitExpr = D->getAnyInitializer(); 1093 1094 if (!InitExpr) { 1095 // This is a tentative definition; tentative definitions are 1096 // implicitly initialized with { 0 }. 1097 // 1098 // Note that tentative definitions are only emitted at the end of 1099 // a translation unit, so they should never have incomplete 1100 // type. In addition, EmitTentativeDefinition makes sure that we 1101 // never attempt to emit a tentative definition if a real one 1102 // exists. A use may still exists, however, so we still may need 1103 // to do a RAUW. 1104 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 1105 Init = EmitNullConstant(D->getType()); 1106 } else { 1107 Init = EmitConstantExpr(InitExpr, D->getType()); 1108 if (!Init) { 1109 QualType T = InitExpr->getType(); 1110 if (D->getType()->isReferenceType()) 1111 T = D->getType(); 1112 1113 if (getLangOptions().CPlusPlus) { 1114 EmitCXXGlobalVarDeclInitFunc(D); 1115 Init = EmitNullConstant(T); 1116 NonConstInit = true; 1117 } else { 1118 ErrorUnsupported(D, "static initializer"); 1119 Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 1120 } 1121 } 1122 } 1123 1124 const llvm::Type* InitType = Init->getType(); 1125 llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 1126 1127 // Strip off a bitcast if we got one back. 1128 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 1129 assert(CE->getOpcode() == llvm::Instruction::BitCast || 1130 // all zero index gep. 1131 CE->getOpcode() == llvm::Instruction::GetElementPtr); 1132 Entry = CE->getOperand(0); 1133 } 1134 1135 // Entry is now either a Function or GlobalVariable. 1136 llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 1137 1138 // We have a definition after a declaration with the wrong type. 1139 // We must make a new GlobalVariable* and update everything that used OldGV 1140 // (a declaration or tentative definition) with the new GlobalVariable* 1141 // (which will be a definition). 1142 // 1143 // This happens if there is a prototype for a global (e.g. 1144 // "extern int x[];") and then a definition of a different type (e.g. 1145 // "int x[10];"). This also happens when an initializer has a different type 1146 // from the type of the global (this happens with unions). 1147 if (GV == 0 || 1148 GV->getType()->getElementType() != InitType || 1149 GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 1150 1151 // Move the old entry aside so that we'll create a new one. 1152 Entry->setName(llvm::StringRef()); 1153 1154 // Make a new global with the correct type, this is now guaranteed to work. 1155 GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 1156 1157 // Replace all uses of the old global with the new global 1158 llvm::Constant *NewPtrForOldDecl = 1159 llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 1160 Entry->replaceAllUsesWith(NewPtrForOldDecl); 1161 1162 // Erase the old global, since it is no longer used. 1163 cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 1164 } 1165 1166 if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 1167 SourceManager &SM = Context.getSourceManager(); 1168 AddAnnotation(EmitAnnotateAttr(GV, AA, 1169 SM.getInstantiationLineNumber(D->getLocation()))); 1170 } 1171 1172 GV->setInitializer(Init); 1173 1174 // If it is safe to mark the global 'constant', do so now. 1175 GV->setConstant(false); 1176 if (!NonConstInit && DeclIsConstantGlobal(Context, D)) 1177 GV->setConstant(true); 1178 1179 GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); 1180 1181 // Set the llvm linkage type as appropriate. 1182 GVALinkage Linkage = GetLinkageForVariable(getContext(), D); 1183 if (Linkage == GVA_Internal) 1184 GV->setLinkage(llvm::Function::InternalLinkage); 1185 else if (D->hasAttr<DLLImportAttr>()) 1186 GV->setLinkage(llvm::Function::DLLImportLinkage); 1187 else if (D->hasAttr<DLLExportAttr>()) 1188 GV->setLinkage(llvm::Function::DLLExportLinkage); 1189 else if (D->hasAttr<WeakAttr>()) { 1190 if (GV->isConstant()) 1191 GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage); 1192 else 1193 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 1194 } else if (Linkage == GVA_TemplateInstantiation || 1195 Linkage == GVA_ExplicitTemplateInstantiation) 1196 // FIXME: It seems like we can provide more specific linkage here 1197 // (LinkOnceODR, WeakODR). 1198 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 1199 else if (!getLangOptions().CPlusPlus && !CodeGenOpts.NoCommon && 1200 !D->hasExternalStorage() && !D->getInit() && 1201 !D->getAttr<SectionAttr>()) { 1202 GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 1203 // common vars aren't constant even if declared const. 1204 GV->setConstant(false); 1205 } else 1206 GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 1207 1208 SetCommonAttributes(D, GV); 1209 1210 // Emit global variable debug information. 1211 if (CGDebugInfo *DI = getDebugInfo()) { 1212 DI->setLocation(D->getLocation()); 1213 DI->EmitGlobalVariable(GV, D); 1214 } 1215 } 1216 1217 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 1218 /// implement a function with no prototype, e.g. "int foo() {}". If there are 1219 /// existing call uses of the old function in the module, this adjusts them to 1220 /// call the new function directly. 1221 /// 1222 /// This is not just a cleanup: the always_inline pass requires direct calls to 1223 /// functions to be able to inline them. If there is a bitcast in the way, it 1224 /// won't inline them. Instcombine normally deletes these calls, but it isn't 1225 /// run at -O0. 1226 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 1227 llvm::Function *NewFn) { 1228 // If we're redefining a global as a function, don't transform it. 1229 llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 1230 if (OldFn == 0) return; 1231 1232 const llvm::Type *NewRetTy = NewFn->getReturnType(); 1233 llvm::SmallVector<llvm::Value*, 4> ArgList; 1234 1235 for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 1236 UI != E; ) { 1237 // TODO: Do invokes ever occur in C code? If so, we should handle them too. 1238 llvm::Value::use_iterator I = UI++; // Increment before the CI is erased. 1239 llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I); 1240 llvm::CallSite CS(CI); 1241 if (!CI || !CS.isCallee(I)) continue; 1242 1243 // If the return types don't match exactly, and if the call isn't dead, then 1244 // we can't transform this call. 1245 if (CI->getType() != NewRetTy && !CI->use_empty()) 1246 continue; 1247 1248 // If the function was passed too few arguments, don't transform. If extra 1249 // arguments were passed, we silently drop them. If any of the types 1250 // mismatch, we don't transform. 1251 unsigned ArgNo = 0; 1252 bool DontTransform = false; 1253 for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1254 E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 1255 if (CS.arg_size() == ArgNo || 1256 CS.getArgument(ArgNo)->getType() != AI->getType()) { 1257 DontTransform = true; 1258 break; 1259 } 1260 } 1261 if (DontTransform) 1262 continue; 1263 1264 // Okay, we can transform this. Create the new call instruction and copy 1265 // over the required information. 1266 ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo); 1267 llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), 1268 ArgList.end(), "", CI); 1269 ArgList.clear(); 1270 if (!NewCall->getType()->isVoidTy()) 1271 NewCall->takeName(CI); 1272 NewCall->setAttributes(CI->getAttributes()); 1273 NewCall->setCallingConv(CI->getCallingConv()); 1274 1275 // Finally, remove the old call, replacing any uses with the new one. 1276 if (!CI->use_empty()) 1277 CI->replaceAllUsesWith(NewCall); 1278 1279 // Copy debug location attached to CI. 1280 if (!CI->getDebugLoc().isUnknown()) 1281 NewCall->setDebugLoc(CI->getDebugLoc()); 1282 CI->eraseFromParent(); 1283 } 1284 } 1285 1286 1287 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 1288 const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 1289 const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD); 1290 getMangleContext().mangleInitDiscriminator(); 1291 // Get or create the prototype for the function. 1292 llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 1293 1294 // Strip off a bitcast if we got one back. 1295 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 1296 assert(CE->getOpcode() == llvm::Instruction::BitCast); 1297 Entry = CE->getOperand(0); 1298 } 1299 1300 1301 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1302 llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 1303 1304 // If the types mismatch then we have to rewrite the definition. 1305 assert(OldFn->isDeclaration() && 1306 "Shouldn't replace non-declaration"); 1307 1308 // F is the Function* for the one with the wrong type, we must make a new 1309 // Function* and update everything that used F (a declaration) with the new 1310 // Function* (which will be a definition). 1311 // 1312 // This happens if there is a prototype for a function 1313 // (e.g. "int f()") and then a definition of a different type 1314 // (e.g. "int f(int x)"). Move the old function aside so that it 1315 // doesn't interfere with GetAddrOfFunction. 1316 OldFn->setName(llvm::StringRef()); 1317 llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 1318 1319 // If this is an implementation of a function without a prototype, try to 1320 // replace any existing uses of the function (which may be calls) with uses 1321 // of the new function 1322 if (D->getType()->isFunctionNoProtoType()) { 1323 ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 1324 OldFn->removeDeadConstantUsers(); 1325 } 1326 1327 // Replace uses of F with the Function we will endow with a body. 1328 if (!Entry->use_empty()) { 1329 llvm::Constant *NewPtrForOldDecl = 1330 llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1331 Entry->replaceAllUsesWith(NewPtrForOldDecl); 1332 } 1333 1334 // Ok, delete the old function now, which is dead. 1335 OldFn->eraseFromParent(); 1336 1337 Entry = NewFn; 1338 } 1339 1340 llvm::Function *Fn = cast<llvm::Function>(Entry); 1341 setFunctionLinkage(D, Fn); 1342 1343 CodeGenFunction(*this).GenerateCode(D, Fn); 1344 1345 SetFunctionDefinitionAttributes(D, Fn); 1346 SetLLVMFunctionAttributesForDefinition(D, Fn); 1347 1348 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1349 AddGlobalCtor(Fn, CA->getPriority()); 1350 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1351 AddGlobalDtor(Fn, DA->getPriority()); 1352 } 1353 1354 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { 1355 const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 1356 const AliasAttr *AA = D->getAttr<AliasAttr>(); 1357 assert(AA && "Not an alias?"); 1358 1359 MangleBuffer MangledName; 1360 getMangledName(MangledName, GD); 1361 1362 // If there is a definition in the module, then it wins over the alias. 1363 // This is dubious, but allow it to be safe. Just ignore the alias. 1364 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 1365 if (Entry && !Entry->isDeclaration()) 1366 return; 1367 1368 const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 1369 1370 // Create a reference to the named value. This ensures that it is emitted 1371 // if a deferred decl. 1372 llvm::Constant *Aliasee; 1373 if (isa<llvm::FunctionType>(DeclTy)) 1374 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl()); 1375 else 1376 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 1377 llvm::PointerType::getUnqual(DeclTy), 0); 1378 1379 // Create the new alias itself, but don't set a name yet. 1380 llvm::GlobalValue *GA = 1381 new llvm::GlobalAlias(Aliasee->getType(), 1382 llvm::Function::ExternalLinkage, 1383 "", Aliasee, &getModule()); 1384 1385 if (Entry) { 1386 assert(Entry->isDeclaration()); 1387 1388 // If there is a declaration in the module, then we had an extern followed 1389 // by the alias, as in: 1390 // extern int test6(); 1391 // ... 1392 // int test6() __attribute__((alias("test7"))); 1393 // 1394 // Remove it and replace uses of it with the alias. 1395 GA->takeName(Entry); 1396 1397 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1398 Entry->getType())); 1399 Entry->eraseFromParent(); 1400 } else { 1401 GA->setName(MangledName.getString()); 1402 } 1403 1404 // Set attributes which are particular to an alias; this is a 1405 // specialization of the attributes which may be set on a global 1406 // variable/function. 1407 if (D->hasAttr<DLLExportAttr>()) { 1408 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1409 // The dllexport attribute is ignored for undefined symbols. 1410 if (FD->getBody()) 1411 GA->setLinkage(llvm::Function::DLLExportLinkage); 1412 } else { 1413 GA->setLinkage(llvm::Function::DLLExportLinkage); 1414 } 1415 } else if (D->hasAttr<WeakAttr>() || 1416 D->hasAttr<WeakRefAttr>() || 1417 D->hasAttr<WeakImportAttr>()) { 1418 GA->setLinkage(llvm::Function::WeakAnyLinkage); 1419 } 1420 1421 SetCommonAttributes(D, GA); 1422 } 1423 1424 /// getBuiltinLibFunction - Given a builtin id for a function like 1425 /// "__builtin_fabsf", return a Function* for "fabsf". 1426 llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, 1427 unsigned BuiltinID) { 1428 assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 1429 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 1430 "isn't a lib fn"); 1431 1432 // Get the name, skip over the __builtin_ prefix (if necessary). 1433 const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 1434 if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 1435 Name += 10; 1436 1437 const llvm::FunctionType *Ty = 1438 cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); 1439 1440 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD)); 1441 } 1442 1443 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 1444 unsigned NumTys) { 1445 return llvm::Intrinsic::getDeclaration(&getModule(), 1446 (llvm::Intrinsic::ID)IID, Tys, NumTys); 1447 } 1448 1449 1450 llvm::Function *CodeGenModule::getMemCpyFn(const llvm::Type *DestType, 1451 const llvm::Type *SrcType, 1452 const llvm::Type *SizeType) { 1453 const llvm::Type *ArgTypes[3] = {DestType, SrcType, SizeType }; 1454 return getIntrinsic(llvm::Intrinsic::memcpy, ArgTypes, 3); 1455 } 1456 1457 llvm::Function *CodeGenModule::getMemMoveFn(const llvm::Type *DestType, 1458 const llvm::Type *SrcType, 1459 const llvm::Type *SizeType) { 1460 const llvm::Type *ArgTypes[3] = {DestType, SrcType, SizeType }; 1461 return getIntrinsic(llvm::Intrinsic::memmove, ArgTypes, 3); 1462 } 1463 1464 llvm::Function *CodeGenModule::getMemSetFn(const llvm::Type *DestType, 1465 const llvm::Type *SizeType) { 1466 const llvm::Type *ArgTypes[2] = { DestType, SizeType }; 1467 return getIntrinsic(llvm::Intrinsic::memset, ArgTypes, 2); 1468 } 1469 1470 static llvm::StringMapEntry<llvm::Constant*> & 1471 GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 1472 const StringLiteral *Literal, 1473 bool TargetIsLSB, 1474 bool &IsUTF16, 1475 unsigned &StringLength) { 1476 unsigned NumBytes = Literal->getByteLength(); 1477 1478 // Check for simple case. 1479 if (!Literal->containsNonAsciiOrNull()) { 1480 StringLength = NumBytes; 1481 return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1482 StringLength)); 1483 } 1484 1485 // Otherwise, convert the UTF8 literals into a byte string. 1486 llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 1487 const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); 1488 UTF16 *ToPtr = &ToBuf[0]; 1489 1490 ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 1491 &ToPtr, ToPtr + NumBytes, 1492 strictConversion); 1493 1494 // Check for conversion failure. 1495 if (Result != conversionOK) { 1496 // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove 1497 // this duplicate code. 1498 assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed"); 1499 StringLength = NumBytes; 1500 return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1501 StringLength)); 1502 } 1503 1504 // ConvertUTF8toUTF16 returns the length in ToPtr. 1505 StringLength = ToPtr - &ToBuf[0]; 1506 1507 // Render the UTF-16 string into a byte array and convert to the target byte 1508 // order. 1509 // 1510 // FIXME: This isn't something we should need to do here. 1511 llvm::SmallString<128> AsBytes; 1512 AsBytes.reserve(StringLength * 2); 1513 for (unsigned i = 0; i != StringLength; ++i) { 1514 unsigned short Val = ToBuf[i]; 1515 if (TargetIsLSB) { 1516 AsBytes.push_back(Val & 0xFF); 1517 AsBytes.push_back(Val >> 8); 1518 } else { 1519 AsBytes.push_back(Val >> 8); 1520 AsBytes.push_back(Val & 0xFF); 1521 } 1522 } 1523 // Append one extra null character, the second is automatically added by our 1524 // caller. 1525 AsBytes.push_back(0); 1526 1527 IsUTF16 = true; 1528 return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 1529 } 1530 1531 llvm::Constant * 1532 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 1533 unsigned StringLength = 0; 1534 bool isUTF16 = false; 1535 llvm::StringMapEntry<llvm::Constant*> &Entry = 1536 GetConstantCFStringEntry(CFConstantStringMap, Literal, 1537 getTargetData().isLittleEndian(), 1538 isUTF16, StringLength); 1539 1540 if (llvm::Constant *C = Entry.getValue()) 1541 return C; 1542 1543 llvm::Constant *Zero = 1544 llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 1545 llvm::Constant *Zeros[] = { Zero, Zero }; 1546 1547 // If we don't already have it, get __CFConstantStringClassReference. 1548 if (!CFConstantStringClassRef) { 1549 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1550 Ty = llvm::ArrayType::get(Ty, 0); 1551 llvm::Constant *GV = CreateRuntimeVariable(Ty, 1552 "__CFConstantStringClassReference"); 1553 // Decay array -> ptr 1554 CFConstantStringClassRef = 1555 llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1556 } 1557 1558 QualType CFTy = getContext().getCFConstantStringType(); 1559 1560 const llvm::StructType *STy = 1561 cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1562 1563 std::vector<llvm::Constant*> Fields(4); 1564 1565 // Class pointer. 1566 Fields[0] = CFConstantStringClassRef; 1567 1568 // Flags. 1569 const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 1570 Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : 1571 llvm::ConstantInt::get(Ty, 0x07C8); 1572 1573 // String pointer. 1574 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1575 1576 llvm::GlobalValue::LinkageTypes Linkage; 1577 bool isConstant; 1578 if (isUTF16) { 1579 // FIXME: why do utf strings get "_" labels instead of "L" labels? 1580 Linkage = llvm::GlobalValue::InternalLinkage; 1581 // Note: -fwritable-strings doesn't make unicode CFStrings writable, but 1582 // does make plain ascii ones writable. 1583 isConstant = true; 1584 } else { 1585 Linkage = llvm::GlobalValue::PrivateLinkage; 1586 isConstant = !Features.WritableStrings; 1587 } 1588 1589 llvm::GlobalVariable *GV = 1590 new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1591 ".str"); 1592 if (isUTF16) { 1593 CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 1594 GV->setAlignment(Align.getQuantity()); 1595 } 1596 Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1597 1598 // String length. 1599 Ty = getTypes().ConvertType(getContext().LongTy); 1600 Fields[3] = llvm::ConstantInt::get(Ty, StringLength); 1601 1602 // The struct. 1603 C = llvm::ConstantStruct::get(STy, Fields); 1604 GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 1605 llvm::GlobalVariable::PrivateLinkage, C, 1606 "_unnamed_cfstring_"); 1607 if (const char *Sect = getContext().Target.getCFStringSection()) 1608 GV->setSection(Sect); 1609 Entry.setValue(GV); 1610 1611 return GV; 1612 } 1613 1614 llvm::Constant * 1615 CodeGenModule::GetAddrOfConstantNSString(const StringLiteral *Literal) { 1616 unsigned StringLength = 0; 1617 bool isUTF16 = false; 1618 llvm::StringMapEntry<llvm::Constant*> &Entry = 1619 GetConstantCFStringEntry(CFConstantStringMap, Literal, 1620 getTargetData().isLittleEndian(), 1621 isUTF16, StringLength); 1622 1623 if (llvm::Constant *C = Entry.getValue()) 1624 return C; 1625 1626 llvm::Constant *Zero = 1627 llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 1628 llvm::Constant *Zeros[] = { Zero, Zero }; 1629 1630 // If we don't already have it, get _NSConstantStringClassReference. 1631 if (!NSConstantStringClassRef) { 1632 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1633 Ty = llvm::ArrayType::get(Ty, 0); 1634 llvm::Constant *GV = CreateRuntimeVariable(Ty, 1635 Features.ObjCNonFragileABI ? 1636 "OBJC_CLASS_$_NSConstantString" : 1637 "_NSConstantStringClassReference"); 1638 // Decay array -> ptr 1639 NSConstantStringClassRef = 1640 llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1641 } 1642 1643 QualType NSTy = getContext().getNSConstantStringType(); 1644 1645 const llvm::StructType *STy = 1646 cast<llvm::StructType>(getTypes().ConvertType(NSTy)); 1647 1648 std::vector<llvm::Constant*> Fields(3); 1649 1650 // Class pointer. 1651 Fields[0] = NSConstantStringClassRef; 1652 1653 // String pointer. 1654 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1655 1656 llvm::GlobalValue::LinkageTypes Linkage; 1657 bool isConstant; 1658 if (isUTF16) { 1659 // FIXME: why do utf strings get "_" labels instead of "L" labels? 1660 Linkage = llvm::GlobalValue::InternalLinkage; 1661 // Note: -fwritable-strings doesn't make unicode NSStrings writable, but 1662 // does make plain ascii ones writable. 1663 isConstant = true; 1664 } else { 1665 Linkage = llvm::GlobalValue::PrivateLinkage; 1666 isConstant = !Features.WritableStrings; 1667 } 1668 1669 llvm::GlobalVariable *GV = 1670 new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1671 ".str"); 1672 if (isUTF16) { 1673 CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 1674 GV->setAlignment(Align.getQuantity()); 1675 } 1676 Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1677 1678 // String length. 1679 const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 1680 Fields[2] = llvm::ConstantInt::get(Ty, StringLength); 1681 1682 // The struct. 1683 C = llvm::ConstantStruct::get(STy, Fields); 1684 GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 1685 llvm::GlobalVariable::PrivateLinkage, C, 1686 "_unnamed_nsstring_"); 1687 // FIXME. Fix section. 1688 if (const char *Sect = 1689 Features.ObjCNonFragileABI 1690 ? getContext().Target.getNSStringNonFragileABISection() 1691 : getContext().Target.getNSStringSection()) 1692 GV->setSection(Sect); 1693 Entry.setValue(GV); 1694 1695 return GV; 1696 } 1697 1698 /// GetStringForStringLiteral - Return the appropriate bytes for a 1699 /// string literal, properly padded to match the literal type. 1700 std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 1701 const char *StrData = E->getStrData(); 1702 unsigned Len = E->getByteLength(); 1703 1704 const ConstantArrayType *CAT = 1705 getContext().getAsConstantArrayType(E->getType()); 1706 assert(CAT && "String isn't pointer or array!"); 1707 1708 // Resize the string to the right size. 1709 std::string Str(StrData, StrData+Len); 1710 uint64_t RealLen = CAT->getSize().getZExtValue(); 1711 1712 if (E->isWide()) 1713 RealLen *= getContext().Target.getWCharWidth()/8; 1714 1715 Str.resize(RealLen, '\0'); 1716 1717 return Str; 1718 } 1719 1720 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a 1721 /// constant array for the given string literal. 1722 llvm::Constant * 1723 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 1724 // FIXME: This can be more efficient. 1725 // FIXME: We shouldn't need to bitcast the constant in the wide string case. 1726 llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); 1727 if (S->isWide()) { 1728 llvm::Type *DestTy = 1729 llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); 1730 C = llvm::ConstantExpr::getBitCast(C, DestTy); 1731 } 1732 return C; 1733 } 1734 1735 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1736 /// array for the given ObjCEncodeExpr node. 1737 llvm::Constant * 1738 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1739 std::string Str; 1740 getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1741 1742 return GetAddrOfConstantCString(Str); 1743 } 1744 1745 1746 /// GenerateWritableString -- Creates storage for a string literal. 1747 static llvm::Constant *GenerateStringLiteral(const std::string &str, 1748 bool constant, 1749 CodeGenModule &CGM, 1750 const char *GlobalName) { 1751 // Create Constant for this string literal. Don't add a '\0'. 1752 llvm::Constant *C = 1753 llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); 1754 1755 // Create a global variable for this string 1756 return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 1757 llvm::GlobalValue::PrivateLinkage, 1758 C, GlobalName); 1759 } 1760 1761 /// GetAddrOfConstantString - Returns a pointer to a character array 1762 /// containing the literal. This contents are exactly that of the 1763 /// given string, i.e. it will not be null terminated automatically; 1764 /// see GetAddrOfConstantCString. Note that whether the result is 1765 /// actually a pointer to an LLVM constant depends on 1766 /// Feature.WriteableStrings. 1767 /// 1768 /// The result has pointer to array type. 1769 llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 1770 const char *GlobalName) { 1771 bool IsConstant = !Features.WritableStrings; 1772 1773 // Get the default prefix if a name wasn't specified. 1774 if (!GlobalName) 1775 GlobalName = ".str"; 1776 1777 // Don't share any string literals if strings aren't constant. 1778 if (!IsConstant) 1779 return GenerateStringLiteral(str, false, *this, GlobalName); 1780 1781 llvm::StringMapEntry<llvm::Constant *> &Entry = 1782 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 1783 1784 if (Entry.getValue()) 1785 return Entry.getValue(); 1786 1787 // Create a global variable for this. 1788 llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 1789 Entry.setValue(C); 1790 return C; 1791 } 1792 1793 /// GetAddrOfConstantCString - Returns a pointer to a character 1794 /// array containing the literal and a terminating '\-' 1795 /// character. The result has pointer to array type. 1796 llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 1797 const char *GlobalName){ 1798 return GetAddrOfConstantString(str + '\0', GlobalName); 1799 } 1800 1801 /// EmitObjCPropertyImplementations - Emit information for synthesized 1802 /// properties for an implementation. 1803 void CodeGenModule::EmitObjCPropertyImplementations(const 1804 ObjCImplementationDecl *D) { 1805 for (ObjCImplementationDecl::propimpl_iterator 1806 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1807 ObjCPropertyImplDecl *PID = *i; 1808 1809 // Dynamic is just for type-checking. 1810 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1811 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1812 1813 // Determine which methods need to be implemented, some may have 1814 // been overridden. Note that ::isSynthesized is not the method 1815 // we want, that just indicates if the decl came from a 1816 // property. What we want to know is if the method is defined in 1817 // this implementation. 1818 if (!D->getInstanceMethod(PD->getGetterName())) 1819 CodeGenFunction(*this).GenerateObjCGetter( 1820 const_cast<ObjCImplementationDecl *>(D), PID); 1821 if (!PD->isReadOnly() && 1822 !D->getInstanceMethod(PD->getSetterName())) 1823 CodeGenFunction(*this).GenerateObjCSetter( 1824 const_cast<ObjCImplementationDecl *>(D), PID); 1825 } 1826 } 1827 } 1828 1829 /// EmitObjCIvarInitializations - Emit information for ivar initialization 1830 /// for an implementation. 1831 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { 1832 if (!Features.NeXTRuntime || D->getNumIvarInitializers() == 0) 1833 return; 1834 DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D)); 1835 assert(DC && "EmitObjCIvarInitializations - null DeclContext"); 1836 IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); 1837 Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 1838 ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(), 1839 D->getLocation(), 1840 D->getLocation(), cxxSelector, 1841 getContext().VoidTy, 0, 1842 DC, true, false, true, 1843 ObjCMethodDecl::Required); 1844 D->addInstanceMethod(DTORMethod); 1845 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); 1846 1847 II = &getContext().Idents.get(".cxx_construct"); 1848 cxxSelector = getContext().Selectors.getSelector(0, &II); 1849 // The constructor returns 'self'. 1850 ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), 1851 D->getLocation(), 1852 D->getLocation(), cxxSelector, 1853 getContext().getObjCIdType(), 0, 1854 DC, true, false, true, 1855 ObjCMethodDecl::Required); 1856 D->addInstanceMethod(CTORMethod); 1857 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); 1858 1859 1860 } 1861 1862 /// EmitNamespace - Emit all declarations in a namespace. 1863 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 1864 for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 1865 I != E; ++I) 1866 EmitTopLevelDecl(*I); 1867 } 1868 1869 // EmitLinkageSpec - Emit all declarations in a linkage spec. 1870 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 1871 if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 1872 LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 1873 ErrorUnsupported(LSD, "linkage spec"); 1874 return; 1875 } 1876 1877 for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 1878 I != E; ++I) 1879 EmitTopLevelDecl(*I); 1880 } 1881 1882 /// EmitTopLevelDecl - Emit code for a single top level declaration. 1883 void CodeGenModule::EmitTopLevelDecl(Decl *D) { 1884 // If an error has occurred, stop code generation, but continue 1885 // parsing and semantic analysis (to ensure all warnings and errors 1886 // are emitted). 1887 if (Diags.hasErrorOccurred()) 1888 return; 1889 1890 // Ignore dependent declarations. 1891 if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 1892 return; 1893 1894 switch (D->getKind()) { 1895 case Decl::CXXConversion: 1896 case Decl::CXXMethod: 1897 case Decl::Function: 1898 // Skip function templates 1899 if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 1900 return; 1901 1902 EmitGlobal(cast<FunctionDecl>(D)); 1903 break; 1904 1905 case Decl::Var: 1906 EmitGlobal(cast<VarDecl>(D)); 1907 break; 1908 1909 // C++ Decls 1910 case Decl::Namespace: 1911 EmitNamespace(cast<NamespaceDecl>(D)); 1912 break; 1913 // No code generation needed. 1914 case Decl::UsingShadow: 1915 case Decl::Using: 1916 case Decl::UsingDirective: 1917 case Decl::ClassTemplate: 1918 case Decl::FunctionTemplate: 1919 case Decl::NamespaceAlias: 1920 break; 1921 case Decl::CXXConstructor: 1922 // Skip function templates 1923 if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 1924 return; 1925 1926 EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 1927 break; 1928 case Decl::CXXDestructor: 1929 EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 1930 break; 1931 1932 case Decl::StaticAssert: 1933 // Nothing to do. 1934 break; 1935 1936 // Objective-C Decls 1937 1938 // Forward declarations, no (immediate) code generation. 1939 case Decl::ObjCClass: 1940 case Decl::ObjCForwardProtocol: 1941 case Decl::ObjCCategory: 1942 case Decl::ObjCInterface: 1943 break; 1944 1945 case Decl::ObjCProtocol: 1946 Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 1947 break; 1948 1949 case Decl::ObjCCategoryImpl: 1950 // Categories have properties but don't support synthesize so we 1951 // can ignore them here. 1952 Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 1953 break; 1954 1955 case Decl::ObjCImplementation: { 1956 ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1957 EmitObjCPropertyImplementations(OMD); 1958 EmitObjCIvarInitializations(OMD); 1959 Runtime->GenerateClass(OMD); 1960 break; 1961 } 1962 case Decl::ObjCMethod: { 1963 ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 1964 // If this is not a prototype, emit the body. 1965 if (OMD->getBody()) 1966 CodeGenFunction(*this).GenerateObjCMethod(OMD); 1967 break; 1968 } 1969 case Decl::ObjCCompatibleAlias: 1970 // compatibility-alias is a directive and has no code gen. 1971 break; 1972 1973 case Decl::LinkageSpec: 1974 EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 1975 break; 1976 1977 case Decl::FileScopeAsm: { 1978 FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 1979 llvm::StringRef AsmString = AD->getAsmString()->getString(); 1980 1981 const std::string &S = getModule().getModuleInlineAsm(); 1982 if (S.empty()) 1983 getModule().setModuleInlineAsm(AsmString); 1984 else 1985 getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); 1986 break; 1987 } 1988 1989 default: 1990 // Make sure we handled everything we should, every other kind is a 1991 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 1992 // function. Need to recode Decl::Kind to do that easily. 1993 assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 1994 } 1995 } 1996