Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 17 | #include "CodeGenTBAA.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 18 | #include "CGCall.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 19 | #include "CGCXXABI.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 20 | #include "CGObjCRuntime.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 21 | #include "TargetInfo.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 24 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | af89689 | 2010-06-21 18:41:26 +0000 | [diff] [blame] | 27 | #include "clang/AST/DeclTemplate.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 28 | #include "clang/AST/Mangle.h" |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 29 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Builtins.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 31 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 32 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 34 | #include "clang/Basic/ConvertUTF.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 35 | #include "llvm/CallingConv.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 36 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | #include "llvm/Intrinsics.h" |
Chris Lattner | d5b8902 | 2009-12-28 21:44:41 +0000 | [diff] [blame] | 38 | #include "llvm/LLVMContext.h" |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Triple.h" |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 40 | #include "llvm/Target/Mangler.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetData.h" |
Gabor Greif | 6ba728d | 2010-04-10 02:56:12 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 78f7ece | 2009-11-07 09:22:46 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | using namespace clang; |
| 45 | using namespace CodeGen; |
| 46 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 47 | static CGCXXABI &createCXXABI(CodeGenModule &CGM) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 48 | switch (CGM.getContext().getTargetInfo().getCXXABI()) { |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 49 | case CXXABI_ARM: return *CreateARMCXXABI(CGM); |
| 50 | case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM); |
| 51 | case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM); |
| 52 | } |
| 53 | |
| 54 | llvm_unreachable("invalid C++ ABI kind"); |
| 55 | return *CreateItaniumCXXABI(CGM); |
| 56 | } |
| 57 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 59 | CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 60 | llvm::Module &M, const llvm::TargetData &TD, |
| 61 | Diagnostic &diags) |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 62 | : Context(C), Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 63 | TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 64 | ABI(createCXXABI(*this)), |
Daniel Dunbar | 08d4792 | 2011-06-21 18:54:39 +0000 | [diff] [blame] | 65 | Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI, CGO), |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 66 | TBAA(0), |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 67 | VTables(*this), ObjCRuntime(0), DebugInfo(0), ARCData(0), RRData(0), |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 68 | CFConstantStringClassRef(0), ConstantStringClassRef(0), |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 69 | NSConstantStringType(0), |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 70 | VMContext(M.getContext()), |
| 71 | NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 72 | BlockObjectAssign(0), BlockObjectDispose(0), |
| 73 | BlockDescriptorType(0), GenericBlockLiteralType(0) { |
David Chisnall | 60be607 | 2011-03-22 21:21:24 +0000 | [diff] [blame] | 74 | if (Features.ObjC1) |
| 75 | createObjCRuntime(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 76 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 77 | // Enable TBAA unless it's suppressed. |
| 78 | if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0) |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 79 | TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(), |
| 80 | ABI.getMangleContext()); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 81 | |
Nick Lewycky | e8ba8d7 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 82 | // If debug info or coverage generation is enabled, create the CGDebugInfo |
| 83 | // object. |
| 84 | if (CodeGenOpts.DebugInfo || CodeGenOpts.EmitGcovArcs || |
| 85 | CodeGenOpts.EmitGcovNotes) |
| 86 | DebugInfo = new CGDebugInfo(*this); |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 87 | |
| 88 | Block.GlobalUniqueCount = 0; |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 89 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 90 | if (C.getLangOptions().ObjCAutoRefCount) |
| 91 | ARCData = new ARCEntrypoints(); |
| 92 | RRData = new RREntrypoints(); |
| 93 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 94 | // Initialize the type cache. |
| 95 | llvm::LLVMContext &LLVMContext = M.getContext(); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 96 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
| 97 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
| 98 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 99 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 100 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 101 | PointerAlignInBytes = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 102 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
| 103 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 104 | IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits); |
| 105 | Int8PtrTy = Int8Ty->getPointerTo(0); |
| 106 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | CodeGenModule::~CodeGenModule() { |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 110 | delete ObjCRuntime; |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 111 | delete &ABI; |
Dan Gohman | 4376c85 | 2010-10-15 18:04:46 +0000 | [diff] [blame] | 112 | delete TBAA; |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 113 | delete DebugInfo; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 114 | delete ARCData; |
| 115 | delete RRData; |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 116 | } |
| 117 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 118 | void CodeGenModule::createObjCRuntime() { |
| 119 | if (!Features.NeXTRuntime) |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 120 | ObjCRuntime = CreateGNUObjCRuntime(*this); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 121 | else |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 122 | ObjCRuntime = CreateMacObjCRuntime(*this); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 125 | void CodeGenModule::Release() { |
Chris Lattner | 82227ff | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 126 | EmitDeferred(); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 127 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 128 | EmitCXXGlobalDtorFunc(); |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 129 | if (ObjCRuntime) |
| 130 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 131 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 132 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 133 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 134 | EmitAnnotations(); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 135 | EmitLLVMUsed(); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 136 | |
John McCall | b259383 | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 137 | SimplifyPersonality(); |
| 138 | |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 139 | if (getCodeGenOpts().EmitDeclMetadata) |
| 140 | EmitDeclMetadata(); |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 141 | |
| 142 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 143 | EmitCoverageFile(); |
Devang Patel | f391dbe | 2011-08-16 20:58:22 +0000 | [diff] [blame] | 144 | |
| 145 | if (DebugInfo) |
| 146 | DebugInfo->finalize(); |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Devang Patel | e80d567 | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 149 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 150 | // Make sure that this type is translated. |
| 151 | Types.UpdateCompletedType(TD); |
| 152 | if (DebugInfo) |
| 153 | DebugInfo->UpdateCompletedType(TD); |
| 154 | } |
| 155 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 156 | llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) { |
| 157 | if (!TBAA) |
| 158 | return 0; |
| 159 | return TBAA->getTBAAInfo(QTy); |
| 160 | } |
| 161 | |
| 162 | void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst, |
| 163 | llvm::MDNode *TBAAInfo) { |
| 164 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); |
| 165 | } |
| 166 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 167 | bool CodeGenModule::isTargetDarwin() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 168 | return getContext().getTargetInfo().getTriple().isOSDarwin(); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 171 | void CodeGenModule::Error(SourceLocation loc, StringRef error) { |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 172 | unsigned diagID = getDiags().getCustomDiagID(Diagnostic::Error, error); |
| 173 | getDiags().Report(Context.getFullLoc(loc), diagID); |
| 174 | } |
| 175 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 176 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 177 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 178 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 179 | bool OmitOnError) { |
| 180 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 181 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 183 | "cannot compile this %0 yet"); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 184 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 185 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 186 | << Msg << S->getSourceRange(); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 188 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 189 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 190 | /// specified decl yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 191 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 192 | bool OmitOnError) { |
| 193 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 194 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 196 | "cannot compile this %0 yet"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 197 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 198 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 199 | } |
| 200 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 201 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
| 202 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 203 | } |
| 204 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 206 | const NamedDecl *D) const { |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 207 | // Internal definitions always have default visibility. |
Chris Lattner | df102fc | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 208 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | 7e714cd | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 209 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 210 | return; |
Daniel Dunbar | 7e714cd | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 212 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 213 | // Set visibility for definitions. |
| 214 | NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 215 | if (LV.visibilityExplicit() || !GV->hasAvailableExternallyLinkage()) |
| 216 | GV->setVisibility(GetLLVMVisibility(LV.visibility())); |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 217 | } |
| 218 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 219 | /// Set the symbol visibility of type information (vtable and RTTI) |
| 220 | /// associated with the given type. |
| 221 | void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV, |
| 222 | const CXXRecordDecl *RD, |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 223 | TypeVisibilityKind TVK) const { |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 224 | setGlobalVisibility(GV, RD); |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 225 | |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 226 | if (!CodeGenOpts.HiddenWeakVTables) |
| 227 | return; |
| 228 | |
Anders Carlsson | 9a86a13 | 2011-01-29 20:36:11 +0000 | [diff] [blame] | 229 | // We never want to drop the visibility for RTTI names. |
| 230 | if (TVK == TVK_ForRTTIName) |
| 231 | return; |
| 232 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 233 | // We want to drop the visibility to hidden for weak type symbols. |
| 234 | // This isn't possible if there might be unresolved references |
| 235 | // elsewhere that rely on this symbol being visible. |
| 236 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 237 | // This should be kept roughly in sync with setThunkVisibility |
| 238 | // in CGVTables.cpp. |
| 239 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 240 | // Preconditions. |
Anders Carlsson | f502d93 | 2011-01-24 00:46:19 +0000 | [diff] [blame] | 241 | if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage || |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 242 | GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility) |
| 243 | return; |
| 244 | |
| 245 | // Don't override an explicit visibility attribute. |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 246 | if (RD->getExplicitVisibility()) |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 247 | return; |
| 248 | |
| 249 | switch (RD->getTemplateSpecializationKind()) { |
| 250 | // We have to disable the optimization if this is an EI definition |
| 251 | // because there might be EI declarations in other shared objects. |
| 252 | case TSK_ExplicitInstantiationDefinition: |
| 253 | case TSK_ExplicitInstantiationDeclaration: |
| 254 | return; |
| 255 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 256 | // Every use of a non-template class's type information has to emit it. |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 257 | case TSK_Undeclared: |
| 258 | break; |
| 259 | |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 260 | // In theory, implicit instantiations can ignore the possibility of |
| 261 | // an explicit instantiation declaration because there necessarily |
| 262 | // must be an EI definition somewhere with default visibility. In |
| 263 | // practice, it's possible to have an explicit instantiation for |
| 264 | // an arbitrary template class, and linkers aren't necessarily able |
| 265 | // to deal with mixed-visibility symbols. |
| 266 | case TSK_ExplicitSpecialization: |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 267 | case TSK_ImplicitInstantiation: |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 268 | if (!CodeGenOpts.HiddenWeakTemplateVTables) |
John McCall | 7a53690 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 269 | return; |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | |
| 273 | // If there's a key function, there may be translation units |
| 274 | // that don't have the key function's definition. But ignore |
| 275 | // this if we're emitting RTTI under -fno-rtti. |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 276 | if (!(TVK != TVK_ForRTTI) || Features.RTTI) { |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 277 | if (Context.getKeyFunction(RD)) |
| 278 | return; |
Anders Carlsson | fa2e99f | 2011-01-29 20:24:48 +0000 | [diff] [blame] | 279 | } |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 280 | |
| 281 | // Otherwise, drop the visibility to hidden. |
| 282 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Rafael Espindola | b1c65ff | 2011-01-11 21:44:37 +0000 | [diff] [blame] | 283 | GV->setUnnamedAddr(true); |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 286 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 287 | const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); |
| 288 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 289 | StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()]; |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 290 | if (!Str.empty()) |
| 291 | return Str; |
| 292 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 293 | if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) { |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 294 | IdentifierInfo *II = ND->getIdentifier(); |
| 295 | assert(II && "Attempt to mangle unnamed decl."); |
| 296 | |
| 297 | Str = II->getName(); |
| 298 | return Str; |
| 299 | } |
| 300 | |
| 301 | llvm::SmallString<256> Buffer; |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 302 | llvm::raw_svector_ostream Out(Buffer); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 303 | if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 304 | getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 305 | else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 306 | getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 307 | else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 308 | getCXXABI().getMangleContext().mangleBlock(BD, Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 309 | else |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 310 | getCXXABI().getMangleContext().mangleName(ND, Out); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 311 | |
| 312 | // Allocate space for the mangled name. |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 313 | Out.flush(); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 314 | size_t Length = Buffer.size(); |
| 315 | char *Name = MangledNamesAllocator.Allocate<char>(Length); |
| 316 | std::copy(Buffer.begin(), Buffer.end(), Name); |
| 317 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 318 | Str = StringRef(Name, Length); |
Anders Carlsson | 793a990 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 319 | |
| 320 | return Str; |
| 321 | } |
| 322 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 323 | void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, |
| 324 | const BlockDecl *BD) { |
| 325 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
| 326 | const Decl *D = GD.getDecl(); |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 327 | llvm::raw_svector_ostream Out(Buffer.getBuffer()); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 328 | if (D == 0) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 329 | MangleCtx.mangleGlobalBlock(BD, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 330 | else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 331 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 332 | else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 333 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 334 | else |
Rafael Espindola | c4850c2 | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 335 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
Anders Carlsson | 9a8822b | 2010-06-09 02:36:32 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 338 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 339 | return getModule().getNamedValue(Name); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 342 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 343 | /// main() runs. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 344 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 345 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 346 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 349 | /// AddGlobalDtor - Add a function to the list that will be called |
| 350 | /// when the module is unloaded. |
| 351 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 352 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 353 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 354 | } |
| 355 | |
| 356 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 357 | // Ctor function type is void()*. |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 358 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 359 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 360 | |
| 361 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 362 | llvm::StructType *CtorStructTy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 363 | llvm::StructType::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 364 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 365 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 366 | // Construct the constructor and destructor arrays. |
| 367 | std::vector<llvm::Constant*> Ctors; |
| 368 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 369 | std::vector<llvm::Constant*> S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 371 | I->second, false)); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 372 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 373 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 374 | } |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 375 | |
| 376 | if (!Ctors.empty()) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 377 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 378 | new llvm::GlobalVariable(TheModule, AT, false, |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 379 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 380 | llvm::ConstantArray::get(AT, Ctors), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 381 | GlobalName); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 382 | } |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 385 | void CodeGenModule::EmitAnnotations() { |
| 386 | if (Annotations.empty()) |
| 387 | return; |
| 388 | |
| 389 | // Create a new global variable for the ConstantStruct in the Module. |
| 390 | llvm::Constant *Array = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 391 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 392 | Annotations.size()), |
| 393 | Annotations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | llvm::GlobalValue *gv = |
| 395 | new llvm::GlobalVariable(TheModule, Array->getType(), false, |
| 396 | llvm::GlobalValue::AppendingLinkage, Array, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 397 | "llvm.global.annotations"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 398 | gv->setSection("llvm.metadata"); |
| 399 | } |
| 400 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 401 | llvm::GlobalValue::LinkageTypes |
| 402 | CodeGenModule::getFunctionLinkage(const FunctionDecl *D) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 403 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 404 | |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 405 | if (Linkage == GVA_Internal) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 406 | return llvm::Function::InternalLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 407 | |
| 408 | if (D->hasAttr<DLLExportAttr>()) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 409 | return llvm::Function::DLLExportLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 410 | |
| 411 | if (D->hasAttr<WeakAttr>()) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 412 | return llvm::Function::WeakAnyLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 413 | |
| 414 | // In C99 mode, 'inline' functions are guaranteed to have a strong |
| 415 | // definition somewhere else, so we can use available_externally linkage. |
| 416 | if (Linkage == GVA_C99Inline) |
Fariborz Jahanian | fd0f89d | 2011-02-04 00:08:13 +0000 | [diff] [blame] | 417 | return llvm::Function::AvailableExternallyLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 418 | |
| 419 | // In C++, the compiler has to emit a definition in every translation unit |
| 420 | // that references the function. We should use linkonce_odr because |
| 421 | // a) if all references in this translation unit are optimized away, we |
| 422 | // don't need to codegen it. b) if the function persists, it needs to be |
| 423 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 424 | // definition is dependable. |
| 425 | if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 426 | return !Context.getLangOptions().AppleKext |
| 427 | ? llvm::Function::LinkOnceODRLinkage |
| 428 | : llvm::Function::InternalLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 429 | |
| 430 | // An explicit instantiation of a template has weak linkage, since |
| 431 | // explicit instantiations can occur in multiple translation units |
| 432 | // and must all be equivalent. However, we are not allowed to |
| 433 | // throw away these explicit instantiations. |
| 434 | if (Linkage == GVA_ExplicitTemplateInstantiation) |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 435 | return !Context.getLangOptions().AppleKext |
| 436 | ? llvm::Function::WeakODRLinkage |
| 437 | : llvm::Function::InternalLinkage; |
Chris Lattner | f8153065 | 2010-06-30 16:58:07 +0000 | [diff] [blame] | 438 | |
| 439 | // Otherwise, we have strong external linkage. |
| 440 | assert(Linkage == GVA_StrongExternal); |
| 441 | return llvm::Function::ExternalLinkage; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 442 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 443 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 444 | |
| 445 | /// SetFunctionDefinitionAttributes - Set attributes for a global. |
| 446 | /// |
| 447 | /// FIXME: This is currently only done for aliases and functions, but not for |
| 448 | /// variables (these details are set in EmitGlobalVarDefinition for variables). |
| 449 | void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, |
| 450 | llvm::GlobalValue *GV) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 451 | SetCommonAttributes(D, GV); |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Daniel Dunbar | 7dbd819 | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 454 | void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | const CGFunctionInfo &Info, |
Daniel Dunbar | 7dbd819 | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 456 | llvm::Function *F) { |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 457 | unsigned CallingConv; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 458 | AttributeListType AttributeList; |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 459 | ConstructAttributeList(Info, D, AttributeList, CallingConv); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 460 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 461 | AttributeList.size())); |
| 462 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 465 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 466 | llvm::Function *F) { |
Rafael Espindola | abca5a1 | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 467 | if (CodeGenOpts.UnwindTables) |
| 468 | F->setHasUWTable(); |
| 469 | |
Daniel Dunbar | 74ac74a | 2009-03-02 04:58:03 +0000 | [diff] [blame] | 470 | if (!Features.Exceptions && !Features.ObjCNonFragileABI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 472 | |
Eli Friedman | 2873aee | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 473 | if (D->hasAttr<NakedAttr>()) { |
| 474 | // Naked implies noinline: we should not be inlining such functions. |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 475 | F->addFnAttr(llvm::Attribute::Naked); |
Eli Friedman | 2873aee | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 476 | F->addFnAttr(llvm::Attribute::NoInline); |
| 477 | } |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 478 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 479 | if (D->hasAttr<NoInlineAttr>()) |
Anders Carlsson | 81ebbde | 2009-02-19 19:22:11 +0000 | [diff] [blame] | 480 | F->addFnAttr(llvm::Attribute::NoInline); |
Mike Stump | f55314d | 2009-10-05 21:58:44 +0000 | [diff] [blame] | 481 | |
Eli Friedman | 2873aee | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 482 | // (noinline wins over always_inline, and we can't specify both in IR) |
| 483 | if (D->hasAttr<AlwaysInlineAttr>() && |
| 484 | !F->hasFnAttr(llvm::Attribute::NoInline)) |
| 485 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
| 486 | |
Rafael Espindola | c5f657f | 2011-01-11 00:26:26 +0000 | [diff] [blame] | 487 | if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) |
| 488 | F->setUnnamedAddr(true); |
| 489 | |
Anders Carlsson | fd01535 | 2009-11-16 16:56:03 +0000 | [diff] [blame] | 490 | if (Features.getStackProtectorMode() == LangOptions::SSPOn) |
| 491 | F->addFnAttr(llvm::Attribute::StackProtect); |
| 492 | else if (Features.getStackProtectorMode() == LangOptions::SSPReq) |
| 493 | F->addFnAttr(llvm::Attribute::StackProtectReq); |
| 494 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 495 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
| 496 | if (alignment) |
| 497 | F->setAlignment(alignment); |
| 498 | |
Mike Stump | fb51ddf | 2009-10-05 22:49:20 +0000 | [diff] [blame] | 499 | // C++ ABI requires 2-byte alignment for member functions. |
Mike Stump | bd6dbd1 | 2009-10-05 23:08:21 +0000 | [diff] [blame] | 500 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 501 | F->setAlignment(2); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | void CodeGenModule::SetCommonAttributes(const Decl *D, |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 505 | llvm::GlobalValue *GV) { |
Anders Carlsson | 934176f | 2011-01-29 19:41:00 +0000 | [diff] [blame] | 506 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 507 | setGlobalVisibility(GV, ND); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 508 | else |
| 509 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 510 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 511 | if (D->hasAttr<UsedAttr>()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 512 | AddUsedGlobal(GV); |
| 513 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 514 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 515 | GV->setSection(SA->getName()); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 516 | |
| 517 | getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 520 | void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, |
| 521 | llvm::Function *F, |
| 522 | const CGFunctionInfo &FI) { |
| 523 | SetLLVMFunctionAttributes(D, FI, F); |
| 524 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 525 | |
| 526 | F->setLinkage(llvm::Function::InternalLinkage); |
| 527 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 528 | SetCommonAttributes(D, F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 531 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, |
Eli Friedman | c6c14d1 | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 532 | llvm::Function *F, |
| 533 | bool IsIncompleteFunction) { |
Peter Collingbourne | 0ac2cf4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 534 | if (unsigned IID = F->getIntrinsicID()) { |
| 535 | // If this is an intrinsic function, set the function's attributes |
| 536 | // to the intrinsic's attributes. |
| 537 | F->setAttributes(llvm::Intrinsic::getAttributes((llvm::Intrinsic::ID)IID)); |
| 538 | return; |
| 539 | } |
| 540 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 541 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 542 | |
Eli Friedman | c6c14d1 | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 543 | if (!IsIncompleteFunction) |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 544 | SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 546 | // Only a few attributes are set on declarations; these may later be |
| 547 | // overridden by a definition. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 549 | if (FD->hasAttr<DLLImportAttr>()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 550 | F->setLinkage(llvm::Function::DLLImportLinkage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | } else if (FD->hasAttr<WeakAttr>() || |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 552 | FD->isWeakImported()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 553 | // "extern_weak" is overloaded in LLVM; we probably should have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | // separate linkage types for this. |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 555 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 556 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | F->setLinkage(llvm::Function::ExternalLinkage); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 558 | |
| 559 | NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility(); |
| 560 | if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) { |
| 561 | F->setVisibility(GetLLVMVisibility(LV.visibility())); |
| 562 | } |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 565 | if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 566 | F->setSection(SA->getName()); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 569 | void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 571 | "Only globals with definition can force usage."); |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 572 | LLVMUsed.push_back(GV); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void CodeGenModule::EmitLLVMUsed() { |
| 576 | // Don't create llvm.used if there is no need. |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 577 | if (LLVMUsed.empty()) |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 578 | return; |
| 579 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 580 | llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 582 | // Convert LLVMUsed to what ConstantArray needs. |
| 583 | std::vector<llvm::Constant*> UsedArray; |
| 584 | UsedArray.resize(LLVMUsed.size()); |
| 585 | for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | UsedArray[i] = |
| 587 | llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 588 | i8PTy); |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 591 | if (UsedArray.empty()) |
| 592 | return; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 593 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
| 595 | llvm::GlobalVariable *GV = |
| 596 | new llvm::GlobalVariable(getModule(), ATy, false, |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 597 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 598 | llvm::ConstantArray::get(ATy, UsedArray), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 599 | "llvm.used"); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 600 | |
| 601 | GV->setSection("llvm.metadata"); |
| 602 | } |
| 603 | |
| 604 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 605 | // Emit code for any potentially referenced deferred decls. Since a |
| 606 | // previously unused static decl may become used during the generation of code |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 607 | // for a static function, iterate until no changes are made. |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 608 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 609 | while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) { |
| 610 | if (!DeferredVTables.empty()) { |
| 611 | const CXXRecordDecl *RD = DeferredVTables.back(); |
| 612 | DeferredVTables.pop_back(); |
| 613 | getVTables().GenerateClassData(getVTableLinkage(RD), RD); |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 614 | continue; |
| 615 | } |
| 616 | |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 617 | GlobalDecl D = DeferredDeclsToEmit.back(); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 618 | DeferredDeclsToEmit.pop_back(); |
| 619 | |
John McCall | c76702c | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 620 | // Check to see if we've already emitted this. This is necessary |
| 621 | // for a couple of reasons: first, decls can end up in the |
| 622 | // deferred-decls queue multiple times, and second, decls can end |
| 623 | // up with definitions in unusual ways (e.g. by an extern inline |
| 624 | // function acquiring a strong function redefinition). Just |
| 625 | // ignore these cases. |
| 626 | // |
| 627 | // TODO: That said, looking this up multiple times is very wasteful. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 628 | StringRef Name = getMangledName(D); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 629 | llvm::GlobalValue *CGRef = GetGlobalValue(Name); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 630 | assert(CGRef && "Deferred decl wasn't referenced?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 632 | if (!CGRef->isDeclaration()) |
| 633 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
John McCall | c76702c | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 635 | // GlobalAlias::isDeclaration() defers to the aliasee, but for our |
| 636 | // purposes an alias counts as a definition. |
| 637 | if (isa<llvm::GlobalAlias>(CGRef)) |
| 638 | continue; |
| 639 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 640 | // Otherwise, emit the definition and move on to the next one. |
| 641 | EmitGlobalDefinition(D); |
| 642 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 646 | /// annotation information for a given GlobalValue. The annotation struct is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
| 648 | /// GlobalValue being annotated. The second field is the constant string |
| 649 | /// created from the AnnotateAttr's annotation. The third field is a constant |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 650 | /// string containing the name of the translation unit. The fourth field is |
| 651 | /// the line number in the file of the annotated value declaration. |
| 652 | /// |
| 653 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 654 | /// appears to. |
| 655 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 657 | const AnnotateAttr *AA, |
| 658 | unsigned LineNo) { |
| 659 | llvm::Module *M = &getModule(); |
| 660 | |
| 661 | // get [N x i8] constants for the annotation string, and the filename string |
| 662 | // which are the 2nd and 3rd elements of the global annotation structure. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 663 | llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | llvm::Constant *anno = llvm::ConstantArray::get(VMContext, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 665 | AA->getAnnotation(), true); |
| 666 | llvm::Constant *unit = llvm::ConstantArray::get(VMContext, |
| 667 | M->getModuleIdentifier(), |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 668 | true); |
| 669 | |
| 670 | // Get the two global values corresponding to the ConstantArrays we just |
| 671 | // created to hold the bytes of the strings. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | llvm::GlobalValue *annoGV = |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 673 | new llvm::GlobalVariable(*M, anno->getType(), false, |
| 674 | llvm::GlobalValue::PrivateLinkage, anno, |
| 675 | GV->getName()); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 676 | // translation unit name string, emitted into the llvm.metadata section. |
| 677 | llvm::GlobalValue *unitGV = |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 678 | new llvm::GlobalVariable(*M, unit->getType(), false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | llvm::GlobalValue::PrivateLinkage, unit, |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 680 | ".str"); |
Rafael Espindola | d3d4e1e | 2011-01-17 22:22:52 +0000 | [diff] [blame] | 681 | unitGV->setUnnamedAddr(true); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 682 | |
Daniel Dunbar | 57d5cee | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 683 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 684 | llvm::Constant *Fields[4] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 685 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 686 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 687 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 688 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 689 | }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 690 | return llvm::ConstantStruct::getAnon(Fields); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Argyrios Kyrtzidis | a6d6af3 | 2010-07-27 22:37:14 +0000 | [diff] [blame] | 693 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Argyrios Kyrtzidis | 90e99a8 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 694 | // Never defer when EmitAllDecls is specified. |
| 695 | if (Features.EmitAllDecls) |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 696 | return false; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 697 | |
Argyrios Kyrtzidis | 4ac7c0b | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 698 | return !getContext().DeclMustBeEmitted(Global); |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 701 | llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
| 702 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
| 703 | assert(AA && "No alias?"); |
| 704 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 705 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 706 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 707 | // See if there is already something with the target's name in the module. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 708 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 709 | |
| 710 | llvm::Constant *Aliasee; |
| 711 | if (isa<llvm::FunctionType>(DeclTy)) |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 712 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(), |
| 713 | /*ForVTable=*/false); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 714 | else |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 715 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 716 | llvm::PointerType::getUnqual(DeclTy), 0); |
| 717 | if (!Entry) { |
| 718 | llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee); |
| 719 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 720 | WeakRefReferences.insert(F); |
| 721 | } |
| 722 | |
| 723 | return Aliasee; |
| 724 | } |
| 725 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 726 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Anders Carlsson | 4a6835e | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 727 | const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 729 | // Weak references don't produce any output by themselves. |
| 730 | if (Global->hasAttr<WeakRefAttr>()) |
| 731 | return; |
| 732 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 733 | // If this is an alias definition (which otherwise looks like a declaration) |
| 734 | // emit it now. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 735 | if (Global->hasAttr<AliasAttr>()) |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 736 | return EmitAliasDefinition(GD); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 738 | // Ignore declarations, they will be emitted on their first use. |
Daniel Dunbar | 5e1e1f9 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 739 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 740 | // Forward declarations are emitted lazily on first use. |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 741 | if (!FD->doesThisDeclarationHaveABody()) { |
| 742 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
| 743 | return; |
| 744 | |
| 745 | const FunctionDecl *InlineDefinition = 0; |
| 746 | FD->getBody(InlineDefinition); |
| 747 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 748 | StringRef MangledName = getMangledName(GD); |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 749 | llvm::StringMap<GlobalDecl>::iterator DDI = |
| 750 | DeferredDecls.find(MangledName); |
| 751 | if (DDI != DeferredDecls.end()) |
| 752 | DeferredDecls.erase(DDI); |
| 753 | EmitGlobalDefinition(InlineDefinition); |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 754 | return; |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 755 | } |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 756 | } else { |
| 757 | const VarDecl *VD = cast<VarDecl>(Global); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 758 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 759 | |
Douglas Gregor | a9a55c0 | 2010-02-06 05:15:45 +0000 | [diff] [blame] | 760 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 761 | return; |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 764 | // Defer code generation when possible if this is a static definition, inline |
| 765 | // function etc. These we only want to emit if they are used. |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 766 | if (!MayDeferGeneration(Global)) { |
| 767 | // Emit the definition if it can't be deferred. |
| 768 | EmitGlobalDefinition(GD); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 769 | return; |
| 770 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 771 | |
| 772 | // If we're deferring emission of a C++ variable with an |
| 773 | // initializer, remember the order in which it appeared in the file. |
| 774 | if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) && |
| 775 | cast<VarDecl>(Global)->hasInit()) { |
| 776 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
| 777 | CXXGlobalInits.push_back(0); |
| 778 | } |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 779 | |
| 780 | // If the value has already been used, add it directly to the |
| 781 | // DeferredDeclsToEmit list. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 782 | StringRef MangledName = getMangledName(GD); |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 783 | if (GetGlobalValue(MangledName)) |
| 784 | DeferredDeclsToEmit.push_back(GD); |
| 785 | else { |
| 786 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 787 | // first use of the mangled name will cause it to move into |
| 788 | // DeferredDeclsToEmit. |
| 789 | DeferredDecls[MangledName] = GD; |
| 790 | } |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 793 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { |
Anders Carlsson | 4a6835e | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 794 | const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 796 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
Anders Carlsson | 8e2efcc | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 797 | Context.getSourceManager(), |
| 798 | "Generating code for declaration"); |
| 799 | |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 800 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 801 | // At -O0, don't generate IR for functions with available_externally |
| 802 | // linkage. |
Douglas Gregor | 7feaeee | 2010-07-15 22:58:18 +0000 | [diff] [blame] | 803 | if (CodeGenOpts.OptimizationLevel == 0 && |
| 804 | !Function->hasAttr<AlwaysInlineAttr>() && |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 805 | getFunctionLinkage(Function) |
| 806 | == llvm::Function::AvailableExternallyLinkage) |
| 807 | return; |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 809 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 810 | // Make sure to emit the definition(s) before we emit the thunks. |
| 811 | // This is necessary for the generation of certain thunks. |
| 812 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method)) |
| 813 | EmitCXXConstructor(CD, GD.getCtorType()); |
| 814 | else if (const CXXDestructorDecl *DD =dyn_cast<CXXDestructorDecl>(Method)) |
| 815 | EmitCXXDestructor(DD, GD.getDtorType()); |
| 816 | else |
| 817 | EmitGlobalFunctionDefinition(GD); |
| 818 | |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 819 | if (Method->isVirtual()) |
| 820 | getVTables().EmitThunks(GD); |
| 821 | |
Eli Friedman | 7dcdf5b | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 822 | return; |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 823 | } |
Chris Lattner | b5e8156 | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 824 | |
Chris Lattner | b5e8156 | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 825 | return EmitGlobalFunctionDefinition(GD); |
Douglas Gregor | 44eac33 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 826 | } |
Chris Lattner | b5e8156 | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 827 | |
| 828 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 829 | return EmitGlobalVarDefinition(VD); |
Chris Lattner | 4357a82 | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 830 | |
| 831 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 834 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 835 | /// module, create and return an llvm Function with the specified type. If there |
| 836 | /// is something in the module with the specified name, return it potentially |
| 837 | /// bitcasted to the right type. |
| 838 | /// |
| 839 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 840 | /// to set the attributes on the function when it is first created. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 841 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 842 | CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 843 | llvm::Type *Ty, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 844 | GlobalDecl D, bool ForVTable, |
| 845 | llvm::Attributes ExtraAttrs) { |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 846 | // Lookup the entry, lazily creating it if necessary. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 847 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 848 | if (Entry) { |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 849 | if (WeakRefReferences.count(Entry)) { |
| 850 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl()); |
| 851 | if (FD && !FD->hasAttr<WeakAttr>()) |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 852 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 853 | |
| 854 | WeakRefReferences.erase(Entry); |
| 855 | } |
| 856 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 857 | if (Entry->getType()->getElementType() == Ty) |
| 858 | return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 860 | // Make sure the result is of the correct type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 861 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 864 | // This function doesn't have a complete type (for example, the return |
| 865 | // type is an incomplete struct). Use a fake type instead, and make |
| 866 | // sure not to try to set attributes. |
| 867 | bool IsIncompleteFunction = false; |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 869 | llvm::FunctionType *FTy; |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 870 | if (isa<llvm::FunctionType>(Ty)) { |
| 871 | FTy = cast<llvm::FunctionType>(Ty); |
| 872 | } else { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 873 | FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 874 | IsIncompleteFunction = true; |
| 875 | } |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 876 | |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 877 | llvm::Function *F = llvm::Function::Create(FTy, |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 878 | llvm::Function::ExternalLinkage, |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 879 | MangledName, &getModule()); |
| 880 | assert(F->getName() == MangledName && "name was uniqued!"); |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 881 | if (D.getDecl()) |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 882 | SetFunctionAttributes(D, F, IsIncompleteFunction); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 883 | if (ExtraAttrs != llvm::Attribute::None) |
| 884 | F->addFnAttr(ExtraAttrs); |
Eli Friedman | 654ad40 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 885 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 886 | // This is the first use or definition of a mangled name. If there is a |
| 887 | // deferred decl with this name, remember that we need to emit it at the end |
| 888 | // of the file. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 889 | llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 890 | if (DDI != DeferredDecls.end()) { |
| 891 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 892 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 893 | DeferredDeclsToEmit.push_back(DDI->second); |
| 894 | DeferredDecls.erase(DDI); |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 895 | |
| 896 | // Otherwise, there are cases we have to worry about where we're |
| 897 | // using a declaration for which we must emit a definition but where |
| 898 | // we might not find a top-level definition: |
| 899 | // - member functions defined inline in their classes |
| 900 | // - friend functions defined inline in some class |
| 901 | // - special member functions with implicit definitions |
| 902 | // If we ever change our AST traversal to walk into class methods, |
| 903 | // this will be unnecessary. |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 904 | // |
| 905 | // We also don't emit a definition for a function if it's going to be an entry |
| 906 | // in a vtable, unless it's already marked as used. |
Rafael Espindola | 01de7a4 | 2011-02-03 06:30:58 +0000 | [diff] [blame] | 907 | } else if (getLangOptions().CPlusPlus && D.getDecl()) { |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 908 | // Look for a declaration that's lexically in a record. |
| 909 | const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl()); |
| 910 | do { |
| 911 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 912 | if (FD->isImplicit() && !ForVTable) { |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 913 | assert(FD->isUsed() && "Sema didn't mark implicit function as used!"); |
Douglas Gregor | a29bf41 | 2011-02-09 02:03:05 +0000 | [diff] [blame] | 914 | DeferredDeclsToEmit.push_back(D.getWithDecl(FD)); |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 915 | break; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 916 | } else if (FD->doesThisDeclarationHaveABody()) { |
Douglas Gregor | a29bf41 | 2011-02-09 02:03:05 +0000 | [diff] [blame] | 917 | DeferredDeclsToEmit.push_back(D.getWithDecl(FD)); |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 918 | break; |
| 919 | } |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 920 | } |
John McCall | bfdcdc8 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 921 | FD = FD->getPreviousDeclaration(); |
| 922 | } while (FD); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 925 | // Make sure the result is of the requested type. |
| 926 | if (!IsIncompleteFunction) { |
| 927 | assert(F->getType()->getElementType() == Ty); |
| 928 | return F; |
| 929 | } |
| 930 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 931 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
John McCall | 784f211 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 932 | return llvm::ConstantExpr::getBitCast(F, PTy); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 935 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 936 | /// non-null, then this function will use the specified type if it has to |
| 937 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 938 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 939 | llvm::Type *Ty, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 940 | bool ForVTable) { |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 941 | // If there was no specific requested type, just convert it now. |
| 942 | if (!Ty) |
Anders Carlsson | 4a6835e | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 943 | Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 944 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 945 | StringRef MangledName = getMangledName(GD); |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 946 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 947 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 948 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 949 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 950 | /// type and name. |
| 951 | llvm::Constant * |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 952 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 953 | StringRef Name, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 954 | llvm::Attributes ExtraAttrs) { |
| 955 | return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
| 956 | ExtraAttrs); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 957 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 959 | static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D, |
| 960 | bool ConstantInit) { |
John McCall | 8878686 | 2010-02-08 21:46:50 +0000 | [diff] [blame] | 961 | if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) |
Eli Friedman | 20e098b | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 962 | return false; |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 963 | |
| 964 | if (Context.getLangOptions().CPlusPlus) { |
| 965 | if (const RecordType *Record |
| 966 | = Context.getBaseElementType(D->getType())->getAs<RecordType>()) |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 967 | return ConstantInit && |
| 968 | cast<CXXRecordDecl>(Record->getDecl())->isPOD() && |
| 969 | !cast<CXXRecordDecl>(Record->getDecl())->hasMutableFields(); |
Eli Friedman | 20e098b | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 970 | } |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 971 | |
Eli Friedman | 20e098b | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 972 | return true; |
| 973 | } |
| 974 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 975 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 976 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 977 | /// is something in the module with the specified name, return it potentially |
| 978 | /// bitcasted to the right type. |
| 979 | /// |
| 980 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 981 | /// to set the attributes on the global when it is first created. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 982 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 983 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 984 | llvm::PointerType *Ty, |
Rafael Espindola | c532b50 | 2011-01-18 21:07:57 +0000 | [diff] [blame] | 985 | const VarDecl *D, |
| 986 | bool UnnamedAddr) { |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 987 | // Lookup the entry, lazily creating it if necessary. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 988 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 989 | if (Entry) { |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 990 | if (WeakRefReferences.count(Entry)) { |
| 991 | if (D && !D->hasAttr<WeakAttr>()) |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 992 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 6a83670 | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 993 | |
| 994 | WeakRefReferences.erase(Entry); |
| 995 | } |
| 996 | |
Rafael Espindola | c532b50 | 2011-01-18 21:07:57 +0000 | [diff] [blame] | 997 | if (UnnamedAddr) |
| 998 | Entry->setUnnamedAddr(true); |
| 999 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1000 | if (Entry->getType() == Ty) |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1001 | return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1003 | // Make sure the result is of the correct type. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1004 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1007 | // This is the first use or definition of a mangled name. If there is a |
| 1008 | // deferred decl with this name, remember that we need to emit it at the end |
| 1009 | // of the file. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1010 | llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1011 | if (DDI != DeferredDecls.end()) { |
| 1012 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 1013 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 1014 | DeferredDeclsToEmit.push_back(DDI->second); |
| 1015 | DeferredDecls.erase(DDI); |
| 1016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | |
| 1018 | llvm::GlobalVariable *GV = |
| 1019 | new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1020 | llvm::GlobalValue::ExternalLinkage, |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1021 | 0, MangledName, 0, |
Eli Friedman | 56ebe50 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 1022 | false, Ty->getAddressSpace()); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1023 | |
| 1024 | // Handle things which are present even on external declarations. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1025 | if (D) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1026 | // FIXME: This code is overly simple and should be merged with other global |
| 1027 | // handling. |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1028 | GV->setConstant(DeclIsConstantGlobal(Context, D, false)); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1029 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1030 | // Set linkage and visibility in case we never see a definition. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1031 | NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); |
| 1032 | if (LV.linkage() != ExternalLinkage) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 1033 | // Don't set internal linkage on declarations. |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1034 | } else { |
| 1035 | if (D->hasAttr<DLLImportAttr>()) |
| 1036 | GV->setLinkage(llvm::GlobalValue::DLLImportLinkage); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1037 | else if (D->hasAttr<WeakAttr>() || D->isWeakImported()) |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1038 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1039 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1040 | // Set visibility on a declaration only if it's explicit. |
| 1041 | if (LV.visibilityExplicit()) |
| 1042 | GV->setVisibility(GetLLVMVisibility(LV.visibility())); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1043 | } |
Eli Friedman | 56ebe50 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 1044 | |
| 1045 | GV->setThreadLocal(D->isThreadSpecified()); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1048 | return GV; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1051 | |
Anders Carlsson | 3bd6202 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1052 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1053 | CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1054 | llvm::Type *Ty, |
Anders Carlsson | 3bd6202 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1055 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 1056 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
| 1057 | llvm::GlobalVariable *OldGV = 0; |
| 1058 | |
| 1059 | |
| 1060 | if (GV) { |
| 1061 | // Check if the variable has the right type. |
| 1062 | if (GV->getType()->getElementType() == Ty) |
| 1063 | return GV; |
| 1064 | |
| 1065 | // Because C++ name mangling, the only way we can end up with an already |
| 1066 | // existing global with the same name is if it has been declared extern "C". |
Anders Carlsson | 96eaf29 | 2011-01-29 18:25:07 +0000 | [diff] [blame] | 1067 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
Anders Carlsson | 3bd6202 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1068 | OldGV = GV; |
| 1069 | } |
| 1070 | |
| 1071 | // Create a new variable. |
| 1072 | GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, |
| 1073 | Linkage, 0, Name); |
| 1074 | |
| 1075 | if (OldGV) { |
| 1076 | // Replace occurrences of the old variable if needed. |
| 1077 | GV->takeName(OldGV); |
| 1078 | |
| 1079 | if (!OldGV->use_empty()) { |
| 1080 | llvm::Constant *NewPtrForOldDecl = |
| 1081 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 1082 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 1083 | } |
| 1084 | |
| 1085 | OldGV->eraseFromParent(); |
| 1086 | } |
| 1087 | |
| 1088 | return GV; |
| 1089 | } |
| 1090 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1091 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 1092 | /// given global variable. If Ty is non-null and if the global doesn't exist, |
| 1093 | /// then it will be greated with the specified type instead of whatever the |
| 1094 | /// normal requested type would be. |
| 1095 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1096 | llvm::Type *Ty) { |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1097 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 1098 | QualType ASTTy = D->getType(); |
| 1099 | if (Ty == 0) |
| 1100 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1102 | llvm::PointerType *PTy = |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1103 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1105 | StringRef MangledName = getMangledName(D); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1106 | return GetOrCreateLLVMGlobal(MangledName, PTy, D); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 1110 | /// specified type and name. |
| 1111 | llvm::Constant * |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1112 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1113 | StringRef Name) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1114 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0, |
Rafael Espindola | c532b50 | 2011-01-18 21:07:57 +0000 | [diff] [blame] | 1115 | true); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1118 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 1119 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 1120 | |
Douglas Gregor | 7520bd1 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 1121 | if (MayDeferGeneration(D)) { |
| 1122 | // If we have not seen a reference to this variable yet, place it |
| 1123 | // into the deferred declarations table to be emitted if needed |
| 1124 | // later. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1125 | StringRef MangledName = getMangledName(D); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1126 | if (!GetGlobalValue(MangledName)) { |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1127 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1128 | return; |
Douglas Gregor | 7520bd1 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | // The tentative definition is the only definition. |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1133 | EmitGlobalVarDefinition(D); |
| 1134 | } |
| 1135 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1136 | void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) { |
| 1137 | if (DefinitionRequired) |
| 1138 | getVTables().GenerateClassData(getVTableLinkage(Class), Class); |
| 1139 | } |
| 1140 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1141 | llvm::GlobalVariable::LinkageTypes |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1142 | CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { |
Eli Friedman | cb5d2d0 | 2011-06-10 21:53:06 +0000 | [diff] [blame] | 1143 | if (RD->getLinkage() != ExternalLinkage) |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1144 | return llvm::GlobalVariable::InternalLinkage; |
| 1145 | |
| 1146 | if (const CXXMethodDecl *KeyFunction |
| 1147 | = RD->getASTContext().getKeyFunction(RD)) { |
| 1148 | // If this class has a key function, use that to determine the linkage of |
| 1149 | // the vtable. |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1150 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1151 | if (KeyFunction->hasBody(Def)) |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1152 | KeyFunction = cast<CXXMethodDecl>(Def); |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1153 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1154 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 1155 | case TSK_Undeclared: |
| 1156 | case TSK_ExplicitSpecialization: |
Anders Carlsson | 6d7f847 | 2011-01-30 20:45:54 +0000 | [diff] [blame] | 1157 | // When compiling with optimizations turned on, we emit all vtables, |
| 1158 | // even if the key function is not defined in the current translation |
| 1159 | // unit. If this is the case, use available_externally linkage. |
| 1160 | if (!Def && CodeGenOpts.OptimizationLevel) |
| 1161 | return llvm::GlobalVariable::AvailableExternallyLinkage; |
| 1162 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1163 | if (KeyFunction->isInlined()) |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1164 | return !Context.getLangOptions().AppleKext ? |
| 1165 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 1166 | llvm::Function::InternalLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1167 | |
| 1168 | return llvm::GlobalVariable::ExternalLinkage; |
| 1169 | |
| 1170 | case TSK_ImplicitInstantiation: |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1171 | return !Context.getLangOptions().AppleKext ? |
| 1172 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 1173 | llvm::Function::InternalLinkage; |
Anders Carlsson | f502d93 | 2011-01-24 00:46:19 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1175 | case TSK_ExplicitInstantiationDefinition: |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1176 | return !Context.getLangOptions().AppleKext ? |
| 1177 | llvm::GlobalVariable::WeakODRLinkage : |
| 1178 | llvm::Function::InternalLinkage; |
Anders Carlsson | f502d93 | 2011-01-24 00:46:19 +0000 | [diff] [blame] | 1179 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1180 | case TSK_ExplicitInstantiationDeclaration: |
| 1181 | // FIXME: Use available_externally linkage. However, this currently |
| 1182 | // breaks LLVM's build due to undefined symbols. |
| 1183 | // return llvm::GlobalVariable::AvailableExternallyLinkage; |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1184 | return !Context.getLangOptions().AppleKext ? |
| 1185 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 1186 | llvm::Function::InternalLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1187 | } |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1190 | if (Context.getLangOptions().AppleKext) |
| 1191 | return llvm::Function::InternalLinkage; |
| 1192 | |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1193 | switch (RD->getTemplateSpecializationKind()) { |
| 1194 | case TSK_Undeclared: |
| 1195 | case TSK_ExplicitSpecialization: |
| 1196 | case TSK_ImplicitInstantiation: |
Douglas Gregor | dffb801 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 1197 | // FIXME: Use available_externally linkage. However, this currently |
| 1198 | // breaks LLVM's build due to undefined symbols. |
| 1199 | // return llvm::GlobalVariable::AvailableExternallyLinkage; |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1200 | case TSK_ExplicitInstantiationDeclaration: |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1201 | return llvm::GlobalVariable::LinkOnceODRLinkage; |
Fariborz Jahanian | 142f9e9 | 2011-02-04 00:01:24 +0000 | [diff] [blame] | 1202 | |
| 1203 | case TSK_ExplicitInstantiationDefinition: |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1204 | return llvm::GlobalVariable::WeakODRLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | // Silence GCC warning. |
Fariborz Jahanian | 53bad4e | 2011-02-04 00:32:39 +0000 | [diff] [blame] | 1208 | return llvm::GlobalVariable::LinkOnceODRLinkage; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1211 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
Ken Dyck | 06f486e | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 1212 | return Context.toCharUnitsFromBits( |
| 1213 | TheTargetData.getTypeStoreSizeInBits(Ty)); |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1216 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 1217 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1218 | QualType ASTTy = D->getType(); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 1219 | bool NonConstInit = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1221 | const Expr *InitExpr = D->getAnyInitializer(); |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (!InitExpr) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 1224 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 03f5ad9 | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1225 | // implicitly initialized with { 0 }. |
| 1226 | // |
| 1227 | // Note that tentative definitions are only emitted at the end of |
| 1228 | // a translation unit, so they should never have incomplete |
| 1229 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 1230 | // never attempt to emit a tentative definition if a real one |
| 1231 | // exists. A use may still exists, however, so we still may need |
| 1232 | // to do a RAUW. |
| 1233 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | b0d0ea0 | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 1234 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1235 | } else { |
Douglas Gregor | c446d18 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 1236 | Init = EmitConstantExpr(InitExpr, D->getType()); |
Eli Friedman | 6e656f4 | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 1237 | if (!Init) { |
Anders Carlsson | 3bb9269 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 1238 | QualType T = InitExpr->getType(); |
Douglas Gregor | c446d18 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 1239 | if (D->getType()->isReferenceType()) |
| 1240 | T = D->getType(); |
| 1241 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1242 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1243 | Init = EmitNullConstant(T); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 1244 | NonConstInit = true; |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1245 | } else { |
| 1246 | ErrorUnsupported(D, "static initializer"); |
| 1247 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 1248 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1249 | } else { |
| 1250 | // We don't need an initializer, so remove the entry for the delayed |
| 1251 | // initializer position (just in case this entry was delayed). |
| 1252 | if (getLangOptions().CPlusPlus) |
| 1253 | DelayedCXXInitPosition.erase(D); |
Eli Friedman | 6e656f4 | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 1254 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1255 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1257 | llvm::Type* InitType = Init->getType(); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1258 | llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1260 | // Strip off a bitcast if we got one back. |
| 1261 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | 9d4a15f | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1262 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 1263 | // all zero index gep. |
| 1264 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1265 | Entry = CE->getOperand(0); |
| 1266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1268 | // Entry is now either a Function or GlobalVariable. |
| 1269 | llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1271 | // We have a definition after a declaration with the wrong type. |
| 1272 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 1273 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 1274 | // (which will be a definition). |
| 1275 | // |
| 1276 | // This happens if there is a prototype for a global (e.g. |
| 1277 | // "extern int x[];") and then a definition of a different type (e.g. |
| 1278 | // "int x[10];"). This also happens when an initializer has a different type |
| 1279 | // from the type of the global (this happens with unions). |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1280 | if (GV == 0 || |
| 1281 | GV->getType()->getElementType() != InitType || |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1282 | GV->getType()->getAddressSpace() != |
| 1283 | getContext().getTargetAddressSpace(ASTTy)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1285 | // Move the old entry aside so that we'll create a new one. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1286 | Entry->setName(StringRef()); |
Daniel Dunbar | 232350d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1288 | // Make a new global with the correct type, this is now guaranteed to work. |
| 1289 | GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1290 | |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1291 | // Replace all uses of the old global with the new global |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1293 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1294 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1295 | |
| 1296 | // Erase the old global, since it is no longer used. |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1297 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 1298 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 1299 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1300 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1301 | SourceManager &SM = Context.getSourceManager(); |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 1302 | AddAnnotation(EmitAnnotateAttr( |
| 1303 | GV, AA, SM.getExpansionLineNumber(D->getLocation()))); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1306 | GV->setInitializer(Init); |
Chris Lattner | e78b86f | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1307 | |
| 1308 | // If it is safe to mark the global 'constant', do so now. |
| 1309 | GV->setConstant(false); |
Douglas Gregor | da55074 | 2011-05-07 22:06:45 +0000 | [diff] [blame] | 1310 | if (!NonConstInit && DeclIsConstantGlobal(Context, D, true)) |
Chris Lattner | e78b86f | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1311 | GV->setConstant(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1313 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1315 | // Set the llvm linkage type as appropriate. |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1316 | llvm::GlobalValue::LinkageTypes Linkage = |
| 1317 | GetLLVMLinkageVarDefinition(D, GV); |
| 1318 | GV->setLinkage(Linkage); |
| 1319 | if (Linkage == llvm::GlobalVariable::CommonLinkage) |
Chris Lattner | e78b86f | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1320 | // common vars aren't constant even if declared const. |
| 1321 | GV->setConstant(false); |
Daniel Dunbar | 7e714cd | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 1322 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1323 | SetCommonAttributes(D, GV); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1324 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1325 | // Emit the initializer function if necessary. |
| 1326 | if (NonConstInit) |
| 1327 | EmitCXXGlobalVarDeclInitFunc(D, GV); |
| 1328 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1329 | // Emit global variable debug information. |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 1330 | if (CGDebugInfo *DI = getModuleDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 1331 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1332 | DI->EmitGlobalVariable(GV, D); |
| 1333 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1334 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1335 | |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1336 | llvm::GlobalValue::LinkageTypes |
| 1337 | CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D, |
| 1338 | llvm::GlobalVariable *GV) { |
| 1339 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(D); |
| 1340 | if (Linkage == GVA_Internal) |
| 1341 | return llvm::Function::InternalLinkage; |
| 1342 | else if (D->hasAttr<DLLImportAttr>()) |
| 1343 | return llvm::Function::DLLImportLinkage; |
| 1344 | else if (D->hasAttr<DLLExportAttr>()) |
| 1345 | return llvm::Function::DLLExportLinkage; |
| 1346 | else if (D->hasAttr<WeakAttr>()) { |
| 1347 | if (GV->isConstant()) |
| 1348 | return llvm::GlobalVariable::WeakODRLinkage; |
| 1349 | else |
| 1350 | return llvm::GlobalVariable::WeakAnyLinkage; |
| 1351 | } else if (Linkage == GVA_TemplateInstantiation || |
| 1352 | Linkage == GVA_ExplicitTemplateInstantiation) |
John McCall | 99ace16 | 2011-04-12 01:46:54 +0000 | [diff] [blame] | 1353 | return llvm::GlobalVariable::WeakODRLinkage; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1354 | else if (!getLangOptions().CPlusPlus && |
| 1355 | ((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) || |
| 1356 | D->getAttr<CommonAttr>()) && |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1357 | !D->hasExternalStorage() && !D->getInit() && |
Fariborz Jahanian | ab27d6e | 2011-06-20 17:50:03 +0000 | [diff] [blame] | 1358 | !D->getAttr<SectionAttr>() && !D->isThreadSpecified() && |
| 1359 | !D->getAttr<WeakImportAttr>()) { |
Fariborz Jahanian | 354e712 | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1360 | // Thread local vars aren't considered common linkage. |
| 1361 | return llvm::GlobalVariable::CommonLinkage; |
| 1362 | } |
| 1363 | return llvm::GlobalVariable::ExternalLinkage; |
| 1364 | } |
| 1365 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1366 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 1367 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 1368 | /// existing call uses of the old function in the module, this adjusts them to |
| 1369 | /// call the new function directly. |
| 1370 | /// |
| 1371 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 1372 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 1373 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 1374 | /// run at -O0. |
| 1375 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 1376 | llvm::Function *NewFn) { |
| 1377 | // If we're redefining a global as a function, don't transform it. |
| 1378 | llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); |
| 1379 | if (OldFn == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1381 | llvm::Type *NewRetTy = NewFn->getReturnType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1382 | SmallVector<llvm::Value*, 4> ArgList; |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1383 | |
| 1384 | for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); |
Benjamin Kramer | dbf02bc | 2010-04-10 11:02:40 +0000 | [diff] [blame] | 1385 | UI != E; ) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1386 | // TODO: Do invokes ever occur in C code? If so, we should handle them too. |
Benjamin Kramer | dbf02bc | 2010-04-10 11:02:40 +0000 | [diff] [blame] | 1387 | llvm::Value::use_iterator I = UI++; // Increment before the CI is erased. |
| 1388 | llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I); |
Gabor Greif | 8670cd3 | 2010-07-28 09:19:33 +0000 | [diff] [blame] | 1389 | if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I) |
Gabor Greif | 35db3b9 | 2010-04-10 03:45:50 +0000 | [diff] [blame] | 1390 | llvm::CallSite CS(CI); |
Benjamin Kramer | dbf02bc | 2010-04-10 11:02:40 +0000 | [diff] [blame] | 1391 | if (!CI || !CS.isCallee(I)) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1393 | // If the return types don't match exactly, and if the call isn't dead, then |
| 1394 | // we can't transform this call. |
| 1395 | if (CI->getType() != NewRetTy && !CI->use_empty()) |
| 1396 | continue; |
| 1397 | |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1398 | // Get the attribute list. |
| 1399 | llvm::SmallVector<llvm::AttributeWithIndex, 8> AttrVec; |
| 1400 | llvm::AttrListPtr AttrList = CI->getAttributes(); |
| 1401 | |
| 1402 | // Get any return attributes. |
| 1403 | llvm::Attributes RAttrs = AttrList.getRetAttributes(); |
| 1404 | |
| 1405 | // Add the return attributes. |
| 1406 | if (RAttrs) |
| 1407 | AttrVec.push_back(llvm::AttributeWithIndex::get(0, RAttrs)); |
| 1408 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1409 | // If the function was passed too few arguments, don't transform. If extra |
| 1410 | // arguments were passed, we silently drop them. If any of the types |
| 1411 | // mismatch, we don't transform. |
| 1412 | unsigned ArgNo = 0; |
| 1413 | bool DontTransform = false; |
| 1414 | for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), |
| 1415 | E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { |
Gabor Greif | 35db3b9 | 2010-04-10 03:45:50 +0000 | [diff] [blame] | 1416 | if (CS.arg_size() == ArgNo || |
| 1417 | CS.getArgument(ArgNo)->getType() != AI->getType()) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1418 | DontTransform = true; |
| 1419 | break; |
| 1420 | } |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1421 | |
| 1422 | // Add any parameter attributes. |
| 1423 | if (llvm::Attributes PAttrs = AttrList.getParamAttributes(ArgNo + 1)) |
| 1424 | AttrVec.push_back(llvm::AttributeWithIndex::get(ArgNo + 1, PAttrs)); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1425 | } |
| 1426 | if (DontTransform) |
| 1427 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1429 | if (llvm::Attributes FnAttrs = AttrList.getFnAttributes()) |
| 1430 | AttrVec.push_back(llvm::AttributeWithIndex::get(~0, FnAttrs)); |
| 1431 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1432 | // Okay, we can transform this. Create the new call instruction and copy |
| 1433 | // over the required information. |
Gabor Greif | 6ba728d | 2010-04-10 02:56:12 +0000 | [diff] [blame] | 1434 | ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1435 | llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList, "", CI); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1436 | ArgList.clear(); |
Benjamin Kramer | ffbb15e | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 1437 | if (!NewCall->getType()->isVoidTy()) |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1438 | NewCall->takeName(CI); |
John McCall | 40f9c30 | 2011-08-03 00:43:55 +0000 | [diff] [blame] | 1439 | NewCall->setAttributes(llvm::AttrListPtr::get(AttrVec.begin(), |
| 1440 | AttrVec.end())); |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1441 | NewCall->setCallingConv(CI->getCallingConv()); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1442 | |
| 1443 | // Finally, remove the old call, replacing any uses with the new one. |
Chris Lattner | 00549fc | 2009-10-14 05:49:21 +0000 | [diff] [blame] | 1444 | if (!CI->use_empty()) |
| 1445 | CI->replaceAllUsesWith(NewCall); |
Devang Patel | 3b122bc | 2009-10-13 17:02:04 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | c603463 | 2010-04-01 06:31:43 +0000 | [diff] [blame] | 1447 | // Copy debug location attached to CI. |
| 1448 | if (!CI->getDebugLoc().isUnknown()) |
| 1449 | NewCall->setDebugLoc(CI->getDebugLoc()); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1450 | CI->eraseFromParent(); |
| 1451 | } |
| 1452 | } |
| 1453 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1454 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1455 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1456 | const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1457 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 1458 | // Compute the function info and LLVM type. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1459 | const CGFunctionInfo &FI = getTypes().getFunctionInfo(GD); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 1460 | bool variadic = false; |
| 1461 | if (const FunctionProtoType *fpt = D->getType()->getAs<FunctionProtoType>()) |
| 1462 | variadic = fpt->isVariadic(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1463 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI, variadic); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 9fa959d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1465 | // Get or create the prototype for the function. |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1466 | llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1468 | // Strip off a bitcast if we got one back. |
| 1469 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 1470 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 1471 | Entry = CE->getOperand(0); |
| 1472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
| 1474 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1475 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1476 | llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Daniel Dunbar | 4274581 | 2009-03-09 23:53:08 +0000 | [diff] [blame] | 1478 | // If the types mismatch then we have to rewrite the definition. |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1479 | assert(OldFn->isDeclaration() && |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1480 | "Shouldn't replace non-declaration"); |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1482 | // F is the Function* for the one with the wrong type, we must make a new |
| 1483 | // Function* and update everything that used F (a declaration) with the new |
| 1484 | // Function* (which will be a definition). |
| 1485 | // |
| 1486 | // This happens if there is a prototype for a function |
| 1487 | // (e.g. "int f()") and then a definition of a different type |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1488 | // (e.g. "int f(int x)"). Move the old function aside so that it |
| 1489 | // doesn't interfere with GetAddrOfFunction. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1490 | OldFn->setName(StringRef()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1491 | llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1493 | // If this is an implementation of a function without a prototype, try to |
| 1494 | // replace any existing uses of the function (which may be calls) with uses |
| 1495 | // of the new function |
Chris Lattner | 9fa959d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1496 | if (D->getType()->isFunctionNoProtoType()) { |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1497 | ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); |
Chris Lattner | 9fa959d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1498 | OldFn->removeDeadConstantUsers(); |
| 1499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1501 | // Replace uses of F with the Function we will endow with a body. |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1502 | if (!Entry->use_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1504 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1505 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 1506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1508 | // Ok, delete the old function now, which is dead. |
Chris Lattner | bdb0132 | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1509 | OldFn->eraseFromParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1511 | Entry = NewFn; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1514 | // We need to set linkage and visibility on the function before |
| 1515 | // generating code for it because various parts of IR generation |
| 1516 | // want to propagate this information down (e.g. to local static |
| 1517 | // declarations). |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1518 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
John McCall | 8b24233 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 1519 | setFunctionLinkage(D, Fn); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1520 | |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1521 | // FIXME: this is redundant with part of SetFunctionDefinitionAttributes |
Anders Carlsson | 0ffeaad | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 1522 | setGlobalVisibility(Fn, D); |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1523 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1524 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1525 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1526 | SetFunctionDefinitionAttributes(D, Fn); |
| 1527 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1529 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1530 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1531 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1532 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1535 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
| 1536 | const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1537 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1538 | assert(AA && "Not an alias?"); |
| 1539 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1540 | StringRef MangledName = getMangledName(GD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1542 | // If there is a definition in the module, then it wins over the alias. |
| 1543 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 1544 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 1545 | if (Entry && !Entry->isDeclaration()) |
| 1546 | return; |
| 1547 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1548 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1549 | |
| 1550 | // Create a reference to the named value. This ensures that it is emitted |
| 1551 | // if a deferred decl. |
| 1552 | llvm::Constant *Aliasee; |
| 1553 | if (isa<llvm::FunctionType>(DeclTy)) |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 1554 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(), |
| 1555 | /*ForVTable=*/false); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1556 | else |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1557 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1558 | llvm::PointerType::getUnqual(DeclTy), 0); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Create the new alias itself, but don't set a name yet. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | llvm::GlobalValue *GA = |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1562 | new llvm::GlobalAlias(Aliasee->getType(), |
| 1563 | llvm::Function::ExternalLinkage, |
| 1564 | "", Aliasee, &getModule()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1566 | if (Entry) { |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1567 | assert(Entry->isDeclaration()); |
| 1568 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1569 | // If there is a declaration in the module, then we had an extern followed |
| 1570 | // by the alias, as in: |
| 1571 | // extern int test6(); |
| 1572 | // ... |
| 1573 | // int test6() __attribute__((alias("test7"))); |
| 1574 | // |
| 1575 | // Remove it and replace uses of it with the alias. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1576 | GA->takeName(Entry); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1578 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1579 | Entry->getType())); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1580 | Entry->eraseFromParent(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1581 | } else { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 1582 | GA->setName(MangledName); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1585 | // Set attributes which are particular to an alias; this is a |
| 1586 | // specialization of the attributes which may be set on a global |
| 1587 | // variable/function. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1588 | if (D->hasAttr<DLLExportAttr>()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1589 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1590 | // The dllexport attribute is ignored for undefined symbols. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1591 | if (FD->hasBody()) |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1592 | GA->setLinkage(llvm::Function::DLLExportLinkage); |
| 1593 | } else { |
| 1594 | GA->setLinkage(llvm::Function::DLLExportLinkage); |
| 1595 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | } else if (D->hasAttr<WeakAttr>() || |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1597 | D->hasAttr<WeakRefAttr>() || |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1598 | D->isWeakImported()) { |
Daniel Dunbar | 7c65e99 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1599 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 1600 | } |
| 1601 | |
| 1602 | SetCommonAttributes(D, GA); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Chris Lattner | b808c95 | 2009-03-22 21:56:56 +0000 | [diff] [blame] | 1605 | /// getBuiltinLibFunction - Given a builtin id for a function like |
| 1606 | /// "__builtin_fabsf", return a Function* for "fabsf". |
Daniel Dunbar | 34771b5 | 2009-09-14 04:33:21 +0000 | [diff] [blame] | 1607 | llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, |
| 1608 | unsigned BuiltinID) { |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1609 | assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1611 | "isn't a lib fn"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1613 | // Get the name, skip over the __builtin_ prefix (if necessary). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1614 | StringRef Name; |
Joerg Sonnenberger | 4fe497d | 2011-05-13 21:12:10 +0000 | [diff] [blame] | 1615 | GlobalDecl D(FD); |
| 1616 | |
| 1617 | // If the builtin has been declared explicitly with an assembler label, |
| 1618 | // use the mangled name. This differs from the plain label on platforms |
| 1619 | // that prefix labels. |
Joerg Sonnenberger | 62c296e | 2011-05-13 22:58:37 +0000 | [diff] [blame] | 1620 | if (FD->hasAttr<AsmLabelAttr>()) |
Joerg Sonnenberger | 4fe497d | 2011-05-13 21:12:10 +0000 | [diff] [blame] | 1621 | Name = getMangledName(D); |
| 1622 | else if (Context.BuiltinInfo.isLibFunction(BuiltinID)) |
| 1623 | Name = Context.BuiltinInfo.GetName(BuiltinID) + 10; |
| 1624 | else |
| 1625 | Name = Context.BuiltinInfo.GetName(BuiltinID); |
| 1626 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1628 | llvm::FunctionType *Ty = |
Eli Friedman | 386ca78 | 2009-12-09 03:05:59 +0000 | [diff] [blame] | 1629 | cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1630 | |
Joerg Sonnenberger | 4fe497d | 2011-05-13 21:12:10 +0000 | [diff] [blame] | 1631 | return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 1634 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 1635 | ArrayRef<llvm::Type*> Tys) { |
Jay Foad | df983a8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1636 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 1637 | Tys); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1638 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1639 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1640 | static llvm::StringMapEntry<llvm::Constant*> & |
| 1641 | GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
| 1642 | const StringLiteral *Literal, |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1643 | bool TargetIsLSB, |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1644 | bool &IsUTF16, |
| 1645 | unsigned &StringLength) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1646 | StringRef String = Literal->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1647 | unsigned NumBytes = String.size(); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1648 | |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 1649 | // Check for simple case. |
| 1650 | if (!Literal->containsNonAsciiOrNull()) { |
| 1651 | StringLength = NumBytes; |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1652 | return Map.GetOrCreateValue(String); |
Daniel Dunbar | f015b03 | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1655 | // Otherwise, convert the UTF8 literals into a byte string. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1656 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1657 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1658 | UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
Fariborz Jahanian | e7ddfb9 | 2010-09-07 19:57:04 +0000 | [diff] [blame] | 1660 | (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 1661 | &ToPtr, ToPtr + NumBytes, |
| 1662 | strictConversion); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1664 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1665 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1666 | |
| 1667 | // Render the UTF-16 string into a byte array and convert to the target byte |
| 1668 | // order. |
| 1669 | // |
| 1670 | // FIXME: This isn't something we should need to do here. |
| 1671 | llvm::SmallString<128> AsBytes; |
| 1672 | AsBytes.reserve(StringLength * 2); |
| 1673 | for (unsigned i = 0; i != StringLength; ++i) { |
| 1674 | unsigned short Val = ToBuf[i]; |
| 1675 | if (TargetIsLSB) { |
| 1676 | AsBytes.push_back(Val & 0xFF); |
| 1677 | AsBytes.push_back(Val >> 8); |
| 1678 | } else { |
| 1679 | AsBytes.push_back(Val >> 8); |
| 1680 | AsBytes.push_back(Val & 0xFF); |
| 1681 | } |
| 1682 | } |
Daniel Dunbar | 434da48 | 2009-08-03 21:47:08 +0000 | [diff] [blame] | 1683 | // Append one extra null character, the second is automatically added by our |
| 1684 | // caller. |
| 1685 | AsBytes.push_back(0); |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1686 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1687 | IsUTF16 = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1688 | return Map.GetOrCreateValue(StringRef(AsBytes.data(), AsBytes.size())); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1691 | static llvm::StringMapEntry<llvm::Constant*> & |
| 1692 | GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
| 1693 | const StringLiteral *Literal, |
| 1694 | unsigned &StringLength) |
| 1695 | { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1696 | StringRef String = Literal->getString(); |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1697 | StringLength = String.size(); |
| 1698 | return Map.GetOrCreateValue(String); |
| 1699 | } |
| 1700 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1701 | llvm::Constant * |
| 1702 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 1703 | unsigned StringLength = 0; |
| 1704 | bool isUTF16 = false; |
| 1705 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
Daniel Dunbar | 70ee975 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1707 | getTargetData().isLittleEndian(), |
| 1708 | isUTF16, StringLength); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1710 | if (llvm::Constant *C = Entry.getValue()) |
| 1711 | return C; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1713 | llvm::Constant *Zero = |
| 1714 | llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1715 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 9d4a15f | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1717 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1718 | if (!CFConstantStringClassRef) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1719 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1720 | Ty = llvm::ArrayType::get(Ty, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | llvm::Constant *GV = CreateRuntimeVariable(Ty, |
Chris Lattner | 9d4a15f | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1722 | "__CFConstantStringClassReference"); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1723 | // Decay array -> ptr |
| 1724 | CFConstantStringClassRef = |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1725 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1728 | QualType CFTy = getContext().getCFConstantStringType(); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1729 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1730 | llvm::StructType *STy = |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1731 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 1732 | |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1733 | std::vector<llvm::Constant*> Fields(4); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1734 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1735 | // Class pointer. |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1736 | Fields[0] = CFConstantStringClassRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1738 | // Flags. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1739 | llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1741 | llvm::ConstantInt::get(Ty, 0x07C8); |
| 1742 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1743 | // String pointer. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1744 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1746 | llvm::GlobalValue::LinkageTypes Linkage; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1747 | bool isConstant; |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1748 | if (isUTF16) { |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1749 | // FIXME: why do utf strings get "_" labels instead of "L" labels? |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1750 | Linkage = llvm::GlobalValue::InternalLinkage; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1751 | // Note: -fwritable-strings doesn't make unicode CFStrings writable, but |
| 1752 | // does make plain ascii ones writable. |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1753 | isConstant = true; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1754 | } else { |
Rafael Espindola | 584acf2 | 2011-03-14 17:55:00 +0000 | [diff] [blame] | 1755 | // FIXME: With OS X ld 123.2 (xcode 4) and LTO we would get a linker error |
| 1756 | // when using private linkage. It is not clear if this is a bug in ld |
| 1757 | // or a reasonable new restriction. |
Rafael Espindola | dc0f137 | 2011-03-14 21:08:19 +0000 | [diff] [blame] | 1758 | Linkage = llvm::GlobalValue::LinkerPrivateLinkage; |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1759 | isConstant = !Features.WritableStrings; |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1760 | } |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1761 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | llvm::GlobalVariable *GV = |
Chris Lattner | 278b9f0 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1763 | new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, |
| 1764 | ".str"); |
Rafael Espindola | b266a1f | 2011-01-17 16:31:00 +0000 | [diff] [blame] | 1765 | GV->setUnnamedAddr(true); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1766 | if (isUTF16) { |
Ken Dyck | 4da244c | 2010-01-26 18:46:23 +0000 | [diff] [blame] | 1767 | CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); |
| 1768 | GV->setAlignment(Align.getQuantity()); |
Daniel Dunbar | f7e903d | 2011-04-12 23:30:52 +0000 | [diff] [blame] | 1769 | } else { |
| 1770 | CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); |
| 1771 | GV->setAlignment(Align.getQuantity()); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1772 | } |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1773 | Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1774 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1775 | // String length. |
| 1776 | Ty = getTypes().ConvertType(getContext().LongTy); |
Anders Carlsson | 5add683 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1777 | Fields[3] = llvm::ConstantInt::get(Ty, StringLength); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1779 | // The struct. |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1780 | C = llvm::ConstantStruct::get(STy, Fields); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 1782 | llvm::GlobalVariable::PrivateLinkage, C, |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1783 | "_unnamed_cfstring_"); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1784 | if (const char *Sect = getContext().getTargetInfo().getCFStringSection()) |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1785 | GV->setSection(Sect); |
Daniel Dunbar | 1d55291 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1786 | Entry.setValue(GV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1788 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1789 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1790 | |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 1791 | static RecordDecl * |
| 1792 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 1793 | DeclContext *DC, IdentifierInfo *Id) { |
| 1794 | SourceLocation Loc; |
| 1795 | if (Ctx.getLangOptions().CPlusPlus) |
| 1796 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
| 1797 | else |
| 1798 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
| 1799 | } |
| 1800 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1801 | llvm::Constant * |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1802 | CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1803 | unsigned StringLength = 0; |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1804 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1805 | GetConstantStringEntry(CFConstantStringMap, Literal, StringLength); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1806 | |
| 1807 | if (llvm::Constant *C = Entry.getValue()) |
| 1808 | return C; |
| 1809 | |
| 1810 | llvm::Constant *Zero = |
| 1811 | llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); |
| 1812 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 1813 | |
Fariborz Jahanian | ec951e0 | 2010-04-23 22:33:39 +0000 | [diff] [blame] | 1814 | // If we don't already have it, get _NSConstantStringClassReference. |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1815 | if (!ConstantStringClassRef) { |
| 1816 | std::string StringClass(getLangOptions().ObjCConstantStringClass); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1817 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1818 | llvm::Constant *GV; |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1819 | if (Features.ObjCNonFragileABI) { |
Fariborz Jahanian | 25dba5d | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 1820 | std::string str = |
| 1821 | StringClass.empty() ? "OBJC_CLASS_$_NSConstantString" |
| 1822 | : "OBJC_CLASS_$_" + StringClass; |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1823 | GV = getObjCRuntime().GetClassGlobal(str); |
| 1824 | // Make sure the result is of the correct type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1825 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1826 | ConstantStringClassRef = |
| 1827 | llvm::ConstantExpr::getBitCast(GV, PTy); |
| 1828 | } else { |
Fariborz Jahanian | 25dba5d | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 1829 | std::string str = |
| 1830 | StringClass.empty() ? "_NSConstantStringClassReference" |
| 1831 | : "_" + StringClass + "ClassReference"; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1832 | llvm::Type *PTy = llvm::ArrayType::get(Ty, 0); |
Fariborz Jahanian | 25dba5d | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 1833 | GV = CreateRuntimeVariable(PTy, str); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1834 | // Decay array -> ptr |
| 1835 | ConstantStringClassRef = |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1836 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1837 | } |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1838 | } |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 1839 | |
| 1840 | if (!NSConstantStringType) { |
| 1841 | // Construct the type for a constant NSString. |
| 1842 | RecordDecl *D = CreateRecordDecl(Context, TTK_Struct, |
| 1843 | Context.getTranslationUnitDecl(), |
| 1844 | &Context.Idents.get("__builtin_NSString")); |
| 1845 | D->startDefinition(); |
| 1846 | |
| 1847 | QualType FieldTypes[3]; |
| 1848 | |
| 1849 | // const int *isa; |
| 1850 | FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst()); |
| 1851 | // const char *str; |
| 1852 | FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst()); |
| 1853 | // unsigned int length; |
| 1854 | FieldTypes[2] = Context.UnsignedIntTy; |
| 1855 | |
| 1856 | // Create fields |
| 1857 | for (unsigned i = 0; i < 3; ++i) { |
| 1858 | FieldDecl *Field = FieldDecl::Create(Context, D, |
| 1859 | SourceLocation(), |
| 1860 | SourceLocation(), 0, |
| 1861 | FieldTypes[i], /*TInfo=*/0, |
| 1862 | /*BitWidth=*/0, |
| 1863 | /*Mutable=*/false, |
| 1864 | /*HasInit=*/false); |
| 1865 | Field->setAccess(AS_public); |
| 1866 | D->addDecl(Field); |
| 1867 | } |
| 1868 | |
| 1869 | D->completeDefinition(); |
| 1870 | QualType NSTy = Context.getTagDeclType(D); |
| 1871 | NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy)); |
| 1872 | } |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1873 | |
| 1874 | std::vector<llvm::Constant*> Fields(3); |
| 1875 | |
| 1876 | // Class pointer. |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1877 | Fields[0] = ConstantStringClassRef; |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1878 | |
| 1879 | // String pointer. |
| 1880 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); |
| 1881 | |
| 1882 | llvm::GlobalValue::LinkageTypes Linkage; |
| 1883 | bool isConstant; |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1884 | Linkage = llvm::GlobalValue::PrivateLinkage; |
| 1885 | isConstant = !Features.WritableStrings; |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1886 | |
| 1887 | llvm::GlobalVariable *GV = |
| 1888 | new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, |
| 1889 | ".str"); |
Rafael Espindola | 803d307 | 2011-01-17 22:11:21 +0000 | [diff] [blame] | 1890 | GV->setUnnamedAddr(true); |
Fariborz Jahanian | cf8e168 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 1891 | CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); |
| 1892 | GV->setAlignment(Align.getQuantity()); |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1893 | Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1894 | |
| 1895 | // String length. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1896 | llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1897 | Fields[2] = llvm::ConstantInt::get(Ty, StringLength); |
| 1898 | |
| 1899 | // The struct. |
Douglas Gregor | 45c4ea7 | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 1900 | C = llvm::ConstantStruct::get(NSConstantStringType, Fields); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1901 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 1902 | llvm::GlobalVariable::PrivateLinkage, C, |
| 1903 | "_unnamed_nsstring_"); |
| 1904 | // FIXME. Fix section. |
Fariborz Jahanian | ec951e0 | 2010-04-23 22:33:39 +0000 | [diff] [blame] | 1905 | if (const char *Sect = |
| 1906 | Features.ObjCNonFragileABI |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1907 | ? getContext().getTargetInfo().getNSStringNonFragileABISection() |
| 1908 | : getContext().getTargetInfo().getNSStringSection()) |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 1909 | GV->setSection(Sect); |
| 1910 | Entry.setValue(GV); |
| 1911 | |
| 1912 | return GV; |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Douglas Gregor | 0815b57 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 1915 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
| 1916 | if (ObjCFastEnumerationStateType.isNull()) { |
| 1917 | RecordDecl *D = CreateRecordDecl(Context, TTK_Struct, |
| 1918 | Context.getTranslationUnitDecl(), |
| 1919 | &Context.Idents.get("__objcFastEnumerationState")); |
| 1920 | D->startDefinition(); |
| 1921 | |
| 1922 | QualType FieldTypes[] = { |
| 1923 | Context.UnsignedLongTy, |
| 1924 | Context.getPointerType(Context.getObjCIdType()), |
| 1925 | Context.getPointerType(Context.UnsignedLongTy), |
| 1926 | Context.getConstantArrayType(Context.UnsignedLongTy, |
| 1927 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 1928 | }; |
| 1929 | |
| 1930 | for (size_t i = 0; i < 4; ++i) { |
| 1931 | FieldDecl *Field = FieldDecl::Create(Context, |
| 1932 | D, |
| 1933 | SourceLocation(), |
| 1934 | SourceLocation(), 0, |
| 1935 | FieldTypes[i], /*TInfo=*/0, |
| 1936 | /*BitWidth=*/0, |
| 1937 | /*Mutable=*/false, |
| 1938 | /*HasInit=*/false); |
| 1939 | Field->setAccess(AS_public); |
| 1940 | D->addDecl(Field); |
| 1941 | } |
| 1942 | |
| 1943 | D->completeDefinition(); |
| 1944 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
| 1945 | } |
| 1946 | |
| 1947 | return ObjCFastEnumerationStateType; |
| 1948 | } |
| 1949 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1950 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1951 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1952 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Ken Dyck | 3b8037a | 2011-01-29 17:53:12 +0000 | [diff] [blame] | 1953 | const ASTContext &Context = getContext(); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1954 | const ConstantArrayType *CAT = |
Ken Dyck | 3b8037a | 2011-01-29 17:53:12 +0000 | [diff] [blame] | 1955 | Context.getAsConstantArrayType(E->getType()); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1956 | assert(CAT && "String isn't pointer or array!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1958 | // Resize the string to the right size. |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1959 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1961 | switch (E->getKind()) { |
| 1962 | case StringLiteral::Ascii: |
| 1963 | case StringLiteral::UTF8: |
| 1964 | break; |
| 1965 | case StringLiteral::Wide: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1966 | RealLen *= Context.getTargetInfo().getWCharWidth() / Context.getCharWidth(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1967 | break; |
| 1968 | case StringLiteral::UTF16: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1969 | RealLen *= Context.getTargetInfo().getChar16Width() / Context.getCharWidth(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1970 | break; |
| 1971 | case StringLiteral::UTF32: |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1972 | RealLen *= Context.getTargetInfo().getChar32Width() / Context.getCharWidth(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1973 | break; |
| 1974 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1976 | std::string Str = E->getString().str(); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1977 | Str.resize(RealLen, '\0'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1979 | return Str; |
| 1980 | } |
| 1981 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1982 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 1983 | /// constant array for the given string literal. |
| 1984 | llvm::Constant * |
| 1985 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 1986 | // FIXME: This can be more efficient. |
Eli Friedman | 7eb79c1 | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 1987 | // FIXME: We shouldn't need to bitcast the constant in the wide string case. |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 1988 | CharUnits Align = getContext().getTypeAlignInChars(S->getType()); |
| 1989 | llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S), |
| 1990 | /* GlobalName */ 0, |
| 1991 | Align.getQuantity()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1992 | if (S->isWide() || S->isUTF16() || S->isUTF32()) { |
Eli Friedman | 7eb79c1 | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 1993 | llvm::Type *DestTy = |
| 1994 | llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); |
| 1995 | C = llvm::ConstantExpr::getBitCast(C, DestTy); |
| 1996 | } |
| 1997 | return C; |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2000 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 2001 | /// array for the given ObjCEncodeExpr node. |
| 2002 | llvm::Constant * |
| 2003 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 2004 | std::string Str; |
| 2005 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | a210f35 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 2006 | |
| 2007 | return GetAddrOfConstantCString(Str); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 2011 | /// GenerateWritableString -- Creates storage for a string literal. |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2012 | static llvm::GlobalVariable *GenerateStringLiteral(StringRef str, |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2013 | bool constant, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 2014 | CodeGenModule &CGM, |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2015 | const char *GlobalName, |
| 2016 | unsigned Alignment) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2017 | // Create Constant for this string literal. Don't add a '\0'. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2018 | llvm::Constant *C = |
| 2019 | llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2021 | // Create a global variable for this string |
Rafael Espindola | 1257bc6 | 2011-01-10 22:34:03 +0000 | [diff] [blame] | 2022 | llvm::GlobalVariable *GV = |
| 2023 | new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, |
| 2024 | llvm::GlobalValue::PrivateLinkage, |
| 2025 | C, GlobalName); |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2026 | GV->setAlignment(Alignment); |
Rafael Espindola | 1257bc6 | 2011-01-10 22:34:03 +0000 | [diff] [blame] | 2027 | GV->setUnnamedAddr(true); |
| 2028 | return GV; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2031 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 2032 | /// containing the literal. This contents are exactly that of the |
| 2033 | /// given string, i.e. it will not be null terminated automatically; |
| 2034 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 2035 | /// actually a pointer to an LLVM constant depends on |
| 2036 | /// Feature.WriteableStrings. |
| 2037 | /// |
| 2038 | /// The result has pointer to array type. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2039 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(StringRef Str, |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2040 | const char *GlobalName, |
| 2041 | unsigned Alignment) { |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2042 | bool IsConstant = !Features.WritableStrings; |
| 2043 | |
| 2044 | // Get the default prefix if a name wasn't specified. |
| 2045 | if (!GlobalName) |
Chris Lattner | 95b851e | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 2046 | GlobalName = ".str"; |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Don't share any string literals if strings aren't constant. |
| 2049 | if (!IsConstant) |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2050 | return GenerateStringLiteral(Str, false, *this, GlobalName, Alignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2052 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
Benjamin Kramer | 9de4342 | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2053 | ConstantStringMap.GetOrCreateValue(Str); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2054 | |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2055 | if (llvm::GlobalVariable *GV = Entry.getValue()) { |
| 2056 | if (Alignment > GV->getAlignment()) { |
| 2057 | GV->setAlignment(Alignment); |
| 2058 | } |
| 2059 | return GV; |
| 2060 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Create a global variable for this. |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2063 | llvm::GlobalVariable *GV = GenerateStringLiteral(Str, true, *this, GlobalName, Alignment); |
| 2064 | Entry.setValue(GV); |
| 2065 | return GV; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2066 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2067 | |
| 2068 | /// GetAddrOfConstantCString - Returns a pointer to a character |
Benjamin Kramer | 9de4342 | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2069 | /// array containing the literal and a terminating '\0' |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2070 | /// character. The result has pointer to array type. |
Benjamin Kramer | 9de4342 | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2071 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &Str, |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2072 | const char *GlobalName, |
| 2073 | unsigned Alignment) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2074 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
John McCall | a5e19c6 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2075 | return GetAddrOfConstantString(StrWithNull, GlobalName, Alignment); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2076 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2077 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2078 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 2079 | /// properties for an implementation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2081 | ObjCImplementationDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2083 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2084 | ObjCPropertyImplDecl *PID = *i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2086 | // Dynamic is just for type-checking. |
| 2087 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2088 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2089 | |
| 2090 | // Determine which methods need to be implemented, some may have |
| 2091 | // been overridden. Note that ::isSynthesized is not the method |
| 2092 | // we want, that just indicates if the decl came from a |
| 2093 | // property. What we want to know is if the method is defined in |
| 2094 | // this implementation. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2095 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 2096 | CodeGenFunction(*this).GenerateObjCGetter( |
| 2097 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2098 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2099 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 2100 | CodeGenFunction(*this).GenerateObjCSetter( |
| 2101 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2106 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2107 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 2108 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2109 | ivar; ivar = ivar->getNextIvar()) |
| 2110 | if (ivar->getType().isDestructedType()) |
| 2111 | return true; |
| 2112 | |
| 2113 | return false; |
| 2114 | } |
| 2115 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2116 | /// EmitObjCIvarInitializations - Emit information for ivar initialization |
| 2117 | /// for an implementation. |
| 2118 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2119 | // We might need a .cxx_destruct even if we don't have any ivar initializers. |
| 2120 | if (needsDestructMethod(D)) { |
| 2121 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
| 2122 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
| 2123 | ObjCMethodDecl *DTORMethod = |
| 2124 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2125 | cxxSelector, getContext().VoidTy, 0, D, |
| 2126 | /*isInstance=*/true, /*isVariadic=*/false, |
| 2127 | /*isSynthesized=*/true, /*isImplicitlyDeclared=*/true, |
| 2128 | /*isDefined=*/false, ObjCMethodDecl::Required); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2129 | D->addInstanceMethod(DTORMethod); |
| 2130 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2131 | D->setHasCXXStructors(true); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | // If the implementation doesn't have any ivar initializers, we don't need |
| 2135 | // a .cxx_construct. |
David Chisnall | 827bbcc | 2011-03-17 14:19:08 +0000 | [diff] [blame] | 2136 | if (D->getNumIvarInitializers() == 0) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2137 | return; |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2138 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2139 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
| 2140 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2141 | // The constructor returns 'self'. |
| 2142 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
| 2143 | D->getLocation(), |
| 2144 | D->getLocation(), cxxSelector, |
| 2145 | getContext().getObjCIdType(), 0, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2146 | D, /*isInstance=*/true, |
| 2147 | /*isVariadic=*/false, |
| 2148 | /*isSynthesized=*/true, |
| 2149 | /*isImplicitlyDeclared=*/true, |
| 2150 | /*isDefined=*/false, |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2151 | ObjCMethodDecl::Required); |
| 2152 | D->addInstanceMethod(CTORMethod); |
| 2153 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2154 | D->setHasCXXStructors(true); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2157 | /// EmitNamespace - Emit all declarations in a namespace. |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2158 | void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2159 | for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2160 | I != E; ++I) |
| 2161 | EmitTopLevelDecl(*I); |
| 2162 | } |
| 2163 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2164 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 2165 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | f976be8 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 2166 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 2167 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2168 | ErrorUnsupported(LSD, "linkage spec"); |
| 2169 | return; |
| 2170 | } |
| 2171 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2172 | for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2173 | I != E; ++I) |
| 2174 | EmitTopLevelDecl(*I); |
| 2175 | } |
| 2176 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2177 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 2178 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 2179 | // If an error has occurred, stop code generation, but continue |
| 2180 | // parsing and semantic analysis (to ensure all warnings and errors |
| 2181 | // are emitted). |
| 2182 | if (Diags.hasErrorOccurred()) |
| 2183 | return; |
| 2184 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2185 | // Ignore dependent declarations. |
| 2186 | if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) |
| 2187 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2189 | switch (D->getKind()) { |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 2190 | case Decl::CXXConversion: |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 2191 | case Decl::CXXMethod: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2192 | case Decl::Function: |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2193 | // Skip function templates |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2194 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 2195 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2196 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 2198 | EmitGlobal(cast<FunctionDecl>(D)); |
| 2199 | break; |
| 2200 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2201 | case Decl::Var: |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 2202 | EmitGlobal(cast<VarDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2203 | break; |
| 2204 | |
John McCall | 26fbc72 | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 2205 | // Indirect fields from global anonymous structs and unions can be |
| 2206 | // ignored; only the actual variable requires IR gen support. |
| 2207 | case Decl::IndirectField: |
| 2208 | break; |
| 2209 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2210 | // C++ Decls |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2211 | case Decl::Namespace: |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2212 | EmitNamespace(cast<NamespaceDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2213 | break; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2214 | // No code generation needed. |
John McCall | 9d0c661 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 2215 | case Decl::UsingShadow: |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2216 | case Decl::Using: |
Douglas Gregor | dd9967a | 2009-09-02 23:49:23 +0000 | [diff] [blame] | 2217 | case Decl::UsingDirective: |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2218 | case Decl::ClassTemplate: |
| 2219 | case Decl::FunctionTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2220 | case Decl::TypeAliasTemplate: |
Anders Carlsson | 018837b | 2009-09-23 19:19:16 +0000 | [diff] [blame] | 2221 | case Decl::NamespaceAlias: |
Douglas Gregor | 6a576ab | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 2222 | case Decl::Block: |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2223 | break; |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2224 | case Decl::CXXConstructor: |
Anders Carlsson | 1fe598c | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 2225 | // Skip function templates |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2226 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 2227 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Anders Carlsson | 1fe598c | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 2228 | return; |
| 2229 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2230 | EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
| 2231 | break; |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 2232 | case Decl::CXXDestructor: |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2233 | if (cast<FunctionDecl>(D)->isLateTemplateParsed()) |
| 2234 | return; |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 2235 | EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
| 2236 | break; |
Anders Carlsson | 36674d2 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 2237 | |
| 2238 | case Decl::StaticAssert: |
| 2239 | // Nothing to do. |
| 2240 | break; |
| 2241 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 2242 | // Objective-C Decls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 2244 | // Forward declarations, no (immediate) code generation. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2245 | case Decl::ObjCClass: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2246 | case Decl::ObjCForwardProtocol: |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 2247 | case Decl::ObjCInterface: |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 2248 | break; |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 2249 | |
Chris Lattner | baf101d | 2011-04-09 07:11:53 +0000 | [diff] [blame] | 2250 | case Decl::ObjCCategory: { |
| 2251 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
| 2252 | if (CD->IsClassExtension() && CD->hasSynthBitfield()) |
| 2253 | Context.ResetObjCLayout(CD->getClassInterface()); |
| 2254 | break; |
| 2255 | } |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 2256 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2257 | case Decl::ObjCProtocol: |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 2258 | ObjCRuntime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | |
| 2261 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2262 | // Categories have properties but don't support synthesize so we |
| 2263 | // can ignore them here. |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 2264 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2265 | break; |
| 2266 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2267 | case Decl::ObjCImplementation: { |
| 2268 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 2269 | if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield()) |
| 2270 | Context.ResetObjCLayout(OMD->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2271 | EmitObjCPropertyImplementations(OMD); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2272 | EmitObjCIvarInitializations(OMD); |
Peter Collingbourne | e926523 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 2273 | ObjCRuntime->GenerateClass(OMD); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2274 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2276 | case Decl::ObjCMethod: { |
| 2277 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 2278 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2279 | if (OMD->getBody()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2280 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 2281 | break; |
| 2282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 2284 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2285 | break; |
| 2286 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2287 | case Decl::LinkageSpec: |
| 2288 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2289 | break; |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2290 | |
| 2291 | case Decl::FileScopeAsm: { |
| 2292 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2293 | StringRef AsmString = AD->getAsmString()->getString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2295 | const std::string &S = getModule().getModuleInlineAsm(); |
| 2296 | if (S.empty()) |
| 2297 | getModule().setModuleInlineAsm(AsmString); |
Chris Lattner | 9f5bff0 | 2011-07-23 20:04:25 +0000 | [diff] [blame] | 2298 | else if (*--S.end() == '\n') |
| 2299 | getModule().setModuleInlineAsm(S + AsmString.str()); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2300 | else |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 2301 | getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2302 | break; |
| 2303 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | |
| 2305 | default: |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2306 | // Make sure we handled everything we should, every other kind is a |
| 2307 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 2308 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2309 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 2310 | } |
| 2311 | } |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2312 | |
| 2313 | /// Turns the given pointer into a constant. |
| 2314 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
| 2315 | const void *Ptr) { |
| 2316 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2317 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2318 | return llvm::ConstantInt::get(i64, PtrInt); |
| 2319 | } |
| 2320 | |
| 2321 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
| 2322 | llvm::NamedMDNode *&GlobalMetadata, |
| 2323 | GlobalDecl D, |
| 2324 | llvm::GlobalValue *Addr) { |
| 2325 | if (!GlobalMetadata) |
| 2326 | GlobalMetadata = |
| 2327 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
| 2328 | |
| 2329 | // TODO: should we report variant information for ctors/dtors? |
| 2330 | llvm::Value *Ops[] = { |
| 2331 | Addr, |
| 2332 | GetPointerConstant(CGM.getLLVMContext(), D.getDecl()) |
| 2333 | }; |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 2334 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /// Emits metadata nodes associating all the global values in the |
| 2338 | /// current module with the Decls they came from. This is useful for |
| 2339 | /// projects using IR gen as a subroutine. |
| 2340 | /// |
| 2341 | /// Since there's currently no way to associate an MDNode directly |
| 2342 | /// with an llvm::GlobalValue, we create a global named metadata |
| 2343 | /// with the name 'clang.global.decl.ptrs'. |
| 2344 | void CodeGenModule::EmitDeclMetadata() { |
| 2345 | llvm::NamedMDNode *GlobalMetadata = 0; |
| 2346 | |
| 2347 | // StaticLocalDeclMap |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2348 | for (llvm::DenseMap<GlobalDecl,StringRef>::iterator |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2349 | I = MangledDeclNames.begin(), E = MangledDeclNames.end(); |
| 2350 | I != E; ++I) { |
| 2351 | llvm::GlobalValue *Addr = getModule().getNamedValue(I->second); |
| 2352 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr); |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | /// Emits metadata nodes for all the local variables in the current |
| 2357 | /// function. |
| 2358 | void CodeGenFunction::EmitDeclMetadata() { |
| 2359 | if (LocalDeclMap.empty()) return; |
| 2360 | |
| 2361 | llvm::LLVMContext &Context = getLLVMContext(); |
| 2362 | |
| 2363 | // Find the unique metadata ID for this name. |
| 2364 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
| 2365 | |
| 2366 | llvm::NamedMDNode *GlobalMetadata = 0; |
| 2367 | |
| 2368 | for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator |
| 2369 | I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) { |
| 2370 | const Decl *D = I->first; |
| 2371 | llvm::Value *Addr = I->second; |
| 2372 | |
| 2373 | if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
| 2374 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 2375 | Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr)); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 2376 | } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
| 2377 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
| 2378 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
| 2379 | } |
| 2380 | } |
| 2381 | } |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 2382 | |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 2383 | void CodeGenModule::EmitCoverageFile() { |
| 2384 | if (!getCodeGenOpts().CoverageFile.empty()) { |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 2385 | if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) { |
| 2386 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
| 2387 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 2388 | llvm::MDString *CoverageFile = |
| 2389 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile); |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 2390 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
| 2391 | llvm::MDNode *CU = CUNode->getOperand(i); |
Nick Lewycky | 3dc0541 | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 2392 | llvm::Value *node[] = { CoverageFile, CU }; |
Nick Lewycky | 5ea4f44 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 2393 | llvm::MDNode *N = llvm::MDNode::get(Ctx, node); |
| 2394 | GCov->addOperand(N); |
| 2395 | } |
| 2396 | } |
| 2397 | } |
| 2398 | } |