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