Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
Daniel Dunbar | 84bb85f | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 19 | #include "Mangle.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 825f6ea | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | dc6262e | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 26 | #include "llvm/CallingConv.h" |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 28 | #include "llvm/Intrinsics.h" |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
| 33 | |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 34 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 35 | llvm::Module &M, const llvm::TargetData &TD, |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 36 | Diagnostic &diags, bool GenerateDebugInfo) |
Mike Stump | d6d0ebe | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 37 | : BlockModule(C, M, TD, Types, *this), Context(C), Features(LO), TheModule(M), |
Mike Stump | 1f010b5 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 38 | TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), |
| 39 | MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) { |
Daniel Dunbar | fc69bde | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 08b05eb | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 41 | if (!Features.ObjC1) |
| 42 | Runtime = 0; |
| 43 | else if (!Features.NeXTRuntime) |
| 44 | Runtime = CreateGNUObjCRuntime(*this); |
| 45 | else if (Features.ObjCNonFragileABI) |
| 46 | Runtime = CreateMacNonFragileABIObjCRuntime(*this); |
| 47 | else |
| 48 | Runtime = CreateMacObjCRuntime(*this); |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 49 | |
| 50 | // If debug info generation is enabled, create the CGDebugInfo object. |
Mike Stump | ef2c82f | 2009-02-13 18:36:05 +0000 | [diff] [blame] | 51 | DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 55 | delete Runtime; |
| 56 | delete DebugInfo; |
| 57 | } |
| 58 | |
| 59 | void CodeGenModule::Release() { |
Daniel Dunbar | 21f3cf7 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 60 | EmitDeferred(); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 61 | EmitAliases(); |
Daniel Dunbar | fc69bde | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 62 | if (Runtime) |
| 63 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 64 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 65 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 66 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 52da5c7 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 67 | EmitAnnotations(); |
Daniel Dunbar | 21f3cf7 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 68 | EmitLLVMUsed(); |
Daniel Dunbar | 18c2ec6 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 71 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 72 | /// specified stmt yet. |
Daniel Dunbar | 49bddf7 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 73 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 74 | bool OmitOnError) { |
| 75 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 76 | return; |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 77 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | c5ba491 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 78 | "cannot compile this %0 yet"); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 79 | std::string Msg = Type; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 80 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 81 | << Msg << S->getSourceRange(); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 82 | } |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 84 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 85 | /// specified decl yet. |
Daniel Dunbar | 49bddf7 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 86 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 87 | bool OmitOnError) { |
| 88 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 89 | return; |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 90 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | c5ba491 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 91 | "cannot compile this %0 yet"); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 92 | std::string Msg = Type; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 93 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 96 | /// setGlobalVisibility - Set the visibility for the given LLVM |
| 97 | /// GlobalValue according to the given clang AST visibility value. |
| 98 | static void setGlobalVisibility(llvm::GlobalValue *GV, |
| 99 | VisibilityAttr::VisibilityTypes Vis) { |
Dan Gohman | 4751a3a | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 100 | switch (Vis) { |
| 101 | default: assert(0 && "Unknown visibility!"); |
| 102 | case VisibilityAttr::DefaultVisibility: |
| 103 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 104 | break; |
| 105 | case VisibilityAttr::HiddenVisibility: |
| 106 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 107 | break; |
| 108 | case VisibilityAttr::ProtectedVisibility: |
| 109 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 114 | /// \brief Retrieves the mangled name for the given declaration. |
| 115 | /// |
| 116 | /// If the given declaration requires a mangled name, returns an |
Chris Lattner | daa199c | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 117 | /// const char* containing the mangled name. Otherwise, returns |
| 118 | /// the unmangled name. |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 119 | /// |
| 120 | /// FIXME: Returning an IdentifierInfo* here is a total hack. We |
| 121 | /// really need some kind of string abstraction that either stores a |
| 122 | /// mangled name or stores an IdentifierInfo*. This will require |
Daniel Dunbar | 7b7f4f4 | 2009-02-18 22:52:09 +0000 | [diff] [blame] | 123 | /// changes to the GlobalDeclMap, too. (I disagree, I think what we |
| 124 | /// actually need is for Sema to provide some notion of which Decls |
| 125 | /// refer to the same semantic decl. We shouldn't need to mangle the |
| 126 | /// names and see what comes out the same to figure this out. - DWD) |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 127 | /// |
| 128 | /// FIXME: Performance here is going to be terribly until we start |
| 129 | /// caching mangled names. However, we should fix the problem above |
| 130 | /// first. |
Douglas Gregor | 3c3c454 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 131 | const char *CodeGenModule::getMangledName(const NamedDecl *ND) { |
Chris Lattner | daa199c | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 132 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 133 | if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) { |
| 134 | assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); |
Chris Lattner | 08b05eb | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 135 | return ND->getNameAsCString(); |
Chris Lattner | daa199c | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | 3c3c454 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 138 | llvm::SmallString<256> Name; |
| 139 | llvm::raw_svector_ostream Out(Name); |
Daniel Dunbar | 947cb1d | 2009-03-05 22:59:19 +0000 | [diff] [blame] | 140 | if (!mangleName(ND, Context, Out)) { |
| 141 | assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); |
Chris Lattner | 08b05eb | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 142 | return ND->getNameAsCString(); |
Daniel Dunbar | 947cb1d | 2009-03-05 22:59:19 +0000 | [diff] [blame] | 143 | } |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 3c3c454 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 145 | Name += '\0'; |
Chris Lattner | 08b05eb | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 146 | return MangledNames.GetOrCreateValue(Name.begin(), Name.end()).getKeyData(); |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 149 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 150 | /// main() runs. |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 151 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 152 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 153 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 156 | /// AddGlobalDtor - Add a function to the list that will be called |
| 157 | /// when the module is unloaded. |
| 158 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 159 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 160 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 161 | } |
| 162 | |
| 163 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 164 | // Ctor function type is void()*. |
| 165 | llvm::FunctionType* CtorFTy = |
| 166 | llvm::FunctionType::get(llvm::Type::VoidTy, |
| 167 | std::vector<const llvm::Type*>(), |
| 168 | false); |
| 169 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
| 170 | |
| 171 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 172 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 173 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 174 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 176 | // Construct the constructor and destructor arrays. |
| 177 | std::vector<llvm::Constant*> Ctors; |
| 178 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 179 | std::vector<llvm::Constant*> S; |
| 180 | S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); |
| 181 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
| 182 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 183 | } |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 184 | |
| 185 | if (!Ctors.empty()) { |
| 186 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
| 187 | new llvm::GlobalVariable(AT, false, |
| 188 | llvm::GlobalValue::AppendingLinkage, |
| 189 | llvm::ConstantArray::get(AT, Ctors), |
| 190 | GlobalName, |
| 191 | &TheModule); |
| 192 | } |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Nate Begeman | 52da5c7 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 195 | void CodeGenModule::EmitAnnotations() { |
| 196 | if (Annotations.empty()) |
| 197 | return; |
| 198 | |
| 199 | // Create a new global variable for the ConstantStruct in the Module. |
| 200 | llvm::Constant *Array = |
| 201 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 202 | Annotations.size()), |
| 203 | Annotations); |
| 204 | llvm::GlobalValue *gv = |
| 205 | new llvm::GlobalVariable(Array->getType(), false, |
| 206 | llvm::GlobalValue::AppendingLinkage, Array, |
| 207 | "llvm.global.annotations", &TheModule); |
| 208 | gv->setSection("llvm.metadata"); |
| 209 | } |
| 210 | |
Daniel Dunbar | 375da3f | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 211 | void CodeGenModule::SetGlobalValueAttributes(const Decl *D, |
| 212 | bool IsInternal, |
| 213 | bool IsInline, |
| 214 | llvm::GlobalValue *GV, |
| 215 | bool ForDefinition) { |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 216 | // FIXME: Set up linkage and many other things. Note, this is a simple |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 217 | // approximation of what we really want. |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 218 | if (!ForDefinition) { |
| 219 | // Only a few attributes are set on declarations. |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 220 | if (D->getAttr<DLLImportAttr>()) { |
| 221 | // The dllimport attribute is overridden by a subsequent declaration as |
| 222 | // dllexport. |
Sebastian Redl | e299eb4 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 223 | if (!D->getAttr<DLLExportAttr>()) { |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 224 | // dllimport attribute can be applied only to function decls, not to |
| 225 | // definitions. |
| 226 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 227 | if (!FD->getBody()) |
| 228 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 229 | } else |
| 230 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
Sebastian Redl | e299eb4 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 231 | } |
Daniel Dunbar | 80f5f99 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 232 | } else if (D->getAttr<WeakAttr>() || |
| 233 | D->getAttr<WeakImportAttr>()) { |
| 234 | // "extern_weak" is overloaded in LLVM; we probably should have |
| 235 | // separate linkage types for this. |
Duncan Sands | 9aa5c26 | 2009-03-11 08:40:02 +0000 | [diff] [blame] | 236 | GV->setLinkage(llvm::Function::ExternalWeakLinkage); |
Daniel Dunbar | 80f5f99 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 237 | } |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 238 | } else { |
| 239 | if (IsInternal) { |
| 240 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 241 | } else { |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 242 | if (D->getAttr<DLLExportAttr>()) { |
| 243 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 244 | // The dllexport attribute is ignored for undefined symbols. |
| 245 | if (FD->getBody()) |
| 246 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 247 | } else |
| 248 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Daniel Dunbar | 80f5f99 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 249 | } else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>() || |
| 250 | IsInline) |
Mike Stump | 36dbf22 | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 251 | GV->setLinkage(llvm::Function::WeakAnyLinkage); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 252 | } |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 253 | } |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 254 | |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 255 | // FIXME: Figure out the relative priority of the attribute, |
| 256 | // -fvisibility, and private_extern. |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 257 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 258 | setGlobalVisibility(GV, attr->getVisibility()); |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 259 | // FIXME: else handle -fvisibility |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | 9750972 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 261 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 262 | GV->setSection(SA->getName()); |
Daniel Dunbar | 375da3f | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 263 | |
| 264 | // Only add to llvm.used when we see a definition, otherwise we |
| 265 | // might add multiple times or risk the value being replaced by a |
| 266 | // subsequent RAUW. |
| 267 | if (ForDefinition) { |
| 268 | if (D->getAttr<UsedAttr>()) |
| 269 | AddUsedGlobal(GV); |
| 270 | } |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Devang Patel | a85a9ef | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 273 | void CodeGenModule::SetFunctionAttributes(const Decl *D, |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 274 | const CGFunctionInfo &Info, |
Daniel Dunbar | 3ef2e85 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 275 | llvm::Function *F) { |
Devang Patel | a85a9ef | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 276 | AttributeListType AttributeList; |
Daniel Dunbar | 6ee022b | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 277 | ConstructAttributeList(Info, D, AttributeList); |
Eli Friedman | fa94dff | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 278 | |
Devang Patel | a85a9ef | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 279 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
| 280 | AttributeList.size())); |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 281 | |
| 282 | // Set the appropriate calling convention for the Function. |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 283 | if (D->getAttr<FastCallAttr>()) |
Anton Korobeynikov | 0ef7c38 | 2008-11-11 20:21:14 +0000 | [diff] [blame] | 284 | F->setCallingConv(llvm::CallingConv::X86_FastCall); |
| 285 | |
| 286 | if (D->getAttr<StdCallAttr>()) |
| 287 | F->setCallingConv(llvm::CallingConv::X86_StdCall); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /// SetFunctionAttributesForDefinition - Set function attributes |
| 291 | /// specific to a function definition. |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 292 | void CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, |
| 293 | llvm::Function *F) { |
| 294 | if (isa<ObjCMethodDecl>(D)) { |
| 295 | SetGlobalValueAttributes(D, true, false, F, true); |
| 296 | } else { |
| 297 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
| 298 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 299 | FD->isInline(), F, true); |
| 300 | } |
| 301 | |
Daniel Dunbar | a47bb8e | 2009-03-02 04:58:03 +0000 | [diff] [blame] | 302 | if (!Features.Exceptions && !Features.ObjCNonFragileABI) |
Daniel Dunbar | 9575eb3 | 2008-09-27 07:16:42 +0000 | [diff] [blame] | 303 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 0a2da71 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 304 | |
| 305 | if (D->getAttr<AlwaysInlineAttr>()) |
| 306 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
Anders Carlsson | 2157eec | 2009-02-19 19:22:11 +0000 | [diff] [blame] | 307 | |
| 308 | if (D->getAttr<NoinlineAttr>()) |
| 309 | F->addFnAttr(llvm::Attribute::NoInline); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, |
| 313 | llvm::Function *F) { |
Daniel Dunbar | 34bda88 | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 314 | SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 315 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 316 | SetFunctionAttributesForDefinition(MD, F); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 320 | llvm::Function *F) { |
Daniel Dunbar | 34bda88 | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 321 | SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 322 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 323 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 324 | FD->isInline(), F, false); |
| 325 | } |
| 326 | |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 327 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 328 | void CodeGenModule::EmitAliases() { |
| 329 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
Daniel Dunbar | 36acae4 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 330 | const ValueDecl *D = Aliases[i]; |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 331 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
| 332 | |
| 333 | // This is something of a hack, if the FunctionDecl got overridden |
| 334 | // then its attributes will be moved to the new declaration. In |
| 335 | // this case the current decl has no alias attribute, but we will |
| 336 | // eventually see it. |
| 337 | if (!AA) |
| 338 | continue; |
| 339 | |
| 340 | const std::string& aliaseeName = AA->getAliasee(); |
Daniel Dunbar | 36acae4 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 341 | llvm::GlobalValue *aliasee = getModule().getNamedValue(aliaseeName); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 342 | if (!aliasee) { |
| 343 | // FIXME: This isn't unsupported, this is just an error, which |
| 344 | // sema should catch, but... |
| 345 | ErrorUnsupported(D, "alias referencing a missing function"); |
| 346 | continue; |
| 347 | } |
| 348 | |
Chris Lattner | 0b50625 | 2009-03-21 08:06:59 +0000 | [diff] [blame] | 349 | const char *MangledName = getMangledName(D); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 350 | llvm::GlobalValue *GA = |
| 351 | new llvm::GlobalAlias(aliasee->getType(), |
| 352 | llvm::Function::ExternalLinkage, |
Chris Lattner | 0b50625 | 2009-03-21 08:06:59 +0000 | [diff] [blame] | 353 | MangledName, aliasee, &getModule()); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 0b50625 | 2009-03-21 08:06:59 +0000 | [diff] [blame] | 355 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 356 | if (Entry) { |
| 357 | // If we created a dummy function for this then replace it. |
| 358 | GA->takeName(Entry); |
| 359 | |
| 360 | llvm::Value *Casted = |
| 361 | llvm::ConstantExpr::getBitCast(GA, Entry->getType()); |
| 362 | Entry->replaceAllUsesWith(Casted); |
| 363 | Entry->eraseFromParent(); |
| 364 | |
| 365 | Entry = GA; |
| 366 | } |
| 367 | |
| 368 | // Alias should never be internal or inline. |
| 369 | SetGlobalValueAttributes(D, false, false, GA, true); |
| 370 | } |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Daniel Dunbar | 21f3cf7 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 373 | void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { |
| 374 | assert(!GV->isDeclaration() && |
| 375 | "Only globals with definition can force usage."); |
| 376 | llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 377 | LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); |
| 378 | } |
| 379 | |
| 380 | void CodeGenModule::EmitLLVMUsed() { |
| 381 | // Don't create llvm.used if there is no need. |
| 382 | if (LLVMUsed.empty()) |
| 383 | return; |
| 384 | |
| 385 | llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), |
| 386 | LLVMUsed.size()); |
| 387 | llvm::GlobalVariable *GV = |
| 388 | new llvm::GlobalVariable(ATy, false, |
| 389 | llvm::GlobalValue::AppendingLinkage, |
| 390 | llvm::ConstantArray::get(ATy, LLVMUsed), |
| 391 | "llvm.used", &getModule()); |
| 392 | |
| 393 | GV->setSection("llvm.metadata"); |
| 394 | } |
| 395 | |
| 396 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 397 | // Emit code for any potentially referenced deferred decls. Since a |
| 398 | // previously unused static decl may become used during the generation of code |
| 399 | // for a static function, iterate until no changes are made. |
| 400 | while (!DeferredDeclsToEmit.empty()) { |
| 401 | const ValueDecl *D = DeferredDeclsToEmit.back(); |
| 402 | DeferredDeclsToEmit.pop_back(); |
| 403 | |
| 404 | // The mangled name for the decl must have been emitted in GlobalDeclMap. |
| 405 | // Look it up to see if it was defined with a stronger definition (e.g. an |
| 406 | // extern inline function with a strong function redefinition). If so, |
| 407 | // just ignore the deferred decl. |
| 408 | llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; |
| 409 | assert(CGRef && "Deferred decl wasn't referenced?"); |
Anders Carlsson | 2e427d5 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 411 | if (!CGRef->isDeclaration()) |
| 412 | continue; |
| 413 | |
| 414 | // Otherwise, emit the definition and move on to the next one. |
| 415 | EmitGlobalDefinition(D); |
| 416 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 419 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 420 | /// annotation information for a given GlobalValue. The annotation struct is |
| 421 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 422 | /// GlobalValue being annotated. The second field is the constant string |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 423 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 424 | /// string containing the name of the translation unit. The fourth field is |
| 425 | /// the line number in the file of the annotated value declaration. |
| 426 | /// |
| 427 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 428 | /// appears to. |
| 429 | /// |
| 430 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 431 | const AnnotateAttr *AA, |
| 432 | unsigned LineNo) { |
| 433 | llvm::Module *M = &getModule(); |
| 434 | |
| 435 | // get [N x i8] constants for the annotation string, and the filename string |
| 436 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 437 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 438 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 439 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 440 | true); |
| 441 | |
| 442 | // Get the two global values corresponding to the ConstantArrays we just |
| 443 | // created to hold the bytes of the strings. |
| 444 | llvm::GlobalValue *annoGV = |
| 445 | new llvm::GlobalVariable(anno->getType(), false, |
| 446 | llvm::GlobalValue::InternalLinkage, anno, |
| 447 | GV->getName() + ".str", M); |
| 448 | // translation unit name string, emitted into the llvm.metadata section. |
| 449 | llvm::GlobalValue *unitGV = |
| 450 | new llvm::GlobalVariable(unit->getType(), false, |
| 451 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 452 | |
| 453 | // Create the ConstantStruct that is the global annotion. |
| 454 | llvm::Constant *Fields[4] = { |
| 455 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 456 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 457 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 458 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 459 | }; |
| 460 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 461 | } |
| 462 | |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 463 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Daniel Dunbar | 375da3f | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 464 | // Never defer when EmitAllDecls is specified or the decl has |
| 465 | // attribute used. |
| 466 | if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 467 | return false; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 468 | |
| 469 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 470 | // Constructors and destructors should never be deferred. |
| 471 | if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) |
| 472 | return false; |
| 473 | |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 474 | // FIXME: What about inline, and/or extern inline? |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 475 | if (FD->getStorageClass() != FunctionDecl::Static) |
| 476 | return false; |
| 477 | } else { |
| 478 | const VarDecl *VD = cast<VarDecl>(Global); |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 479 | assert(VD->isFileVarDecl() && "Invalid decl"); |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 480 | |
| 481 | if (VD->getStorageClass() != VarDecl::Static) |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
Daniel Dunbar | 36acae4 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 489 | // Aliases are deferred until code for everything else has been |
| 490 | // emitted. |
| 491 | if (Global->getAttr<AliasAttr>()) { |
| 492 | Aliases.push_back(Global); |
| 493 | return; |
| 494 | } |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 496 | // Ignore declarations, they will be emitted on their first use. |
Daniel Dunbar | 36acae4 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 497 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 498 | // Forward declarations are emitted lazily on first use. |
| 499 | if (!FD->isThisDeclarationADefinition()) |
| 500 | return; |
Daniel Dunbar | 21f3cf7 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 501 | } else { |
| 502 | const VarDecl *VD = cast<VarDecl>(Global); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 503 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 504 | |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 505 | // Forward declarations are emitted lazily on first use. |
Daniel Dunbar | 0c79d78 | 2009-02-13 22:49:13 +0000 | [diff] [blame] | 506 | if (!VD->getInit() && VD->hasExternalStorage()) |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 507 | return; |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 510 | // Defer code generation when possible if this is a static definition, inline |
| 511 | // function etc. These we only want to emit if they are used. |
Daniel Dunbar | f3482b3 | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 512 | if (MayDeferGeneration(Global)) { |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 513 | // If the value has already been used, add it directly to the |
| 514 | // DeferredDeclsToEmit list. |
| 515 | const char *MangledName = getMangledName(Global); |
| 516 | if (GlobalDeclMap.count(MangledName)) |
| 517 | DeferredDeclsToEmit.push_back(Global); |
| 518 | else { |
| 519 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 520 | // first use of the mangled name will cause it to move into |
| 521 | // DeferredDeclsToEmit. |
| 522 | DeferredDecls[MangledName] = Global; |
| 523 | } |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
| 527 | // Otherwise emit the definition. |
| 528 | EmitGlobalDefinition(Global); |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 531 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 532 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 533 | EmitGlobalFunctionDefinition(FD); |
| 534 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 535 | EmitGlobalVarDefinition(VD); |
| 536 | } else { |
| 537 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 538 | } |
| 539 | } |
| 540 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 541 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 542 | /// module, create and return an llvm Function with the specified type. If there |
| 543 | /// is something in the module with the specified name, return it potentially |
| 544 | /// bitcasted to the right type. |
| 545 | /// |
| 546 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 547 | /// to set the attributes on the function when it is first created. |
| 548 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, |
| 549 | const llvm::Type *Ty, |
| 550 | const FunctionDecl *D) { |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 551 | // Lookup the entry, lazily creating it if necessary. |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 552 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
| 553 | if (Entry) { |
| 554 | if (Entry->getType()->getElementType() == Ty) |
| 555 | return Entry; |
| 556 | |
| 557 | // Make sure the result is of the correct type. |
| 558 | const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
| 559 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
| 560 | } |
| 561 | |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 562 | // This is the first use or definition of a mangled name. If there is a |
| 563 | // deferred decl with this name, remember that we need to emit it at the end |
| 564 | // of the file. |
| 565 | llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 566 | DeferredDecls.find(MangledName); |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 567 | if (DDI != DeferredDecls.end()) { |
| 568 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 569 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 570 | DeferredDeclsToEmit.push_back(DDI->second); |
| 571 | DeferredDecls.erase(DDI); |
| 572 | } |
| 573 | |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 574 | // This function doesn't have a complete type (for example, the return |
| 575 | // type is an incomplete struct). Use a fake type instead, and make |
| 576 | // sure not to try to set attributes. |
| 577 | bool ShouldSetAttributes = true; |
| 578 | if (!isa<llvm::FunctionType>(Ty)) { |
| 579 | Ty = llvm::FunctionType::get(llvm::Type::VoidTy, |
| 580 | std::vector<const llvm::Type*>(), false); |
| 581 | ShouldSetAttributes = false; |
| 582 | } |
| 583 | llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), |
| 584 | llvm::Function::ExternalLinkage, |
Chris Lattner | 68306b3 | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 585 | "", &getModule()); |
| 586 | F->setName(MangledName); |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 587 | if (D && ShouldSetAttributes) |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 588 | SetFunctionAttributes(D, F); |
| 589 | Entry = F; |
| 590 | return F; |
| 591 | } |
| 592 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 593 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 594 | /// non-null, then this function will use the specified type if it has to |
| 595 | /// create it (this occurs when we see a definition of the function). |
| 596 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D, |
| 597 | const llvm::Type *Ty) { |
| 598 | // If there was no specific requested type, just convert it now. |
| 599 | if (!Ty) |
| 600 | Ty = getTypes().ConvertType(D->getType()); |
| 601 | return GetOrCreateLLVMFunction(getMangledName(D), Ty, D); |
| 602 | } |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 603 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 604 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 605 | /// type and name. |
| 606 | llvm::Constant * |
| 607 | CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, |
| 608 | const char *Name) { |
| 609 | // Convert Name to be a uniqued string from the IdentifierInfo table. |
| 610 | Name = getContext().Idents.get(Name).getName(); |
| 611 | return GetOrCreateLLVMFunction(Name, FTy, 0); |
| 612 | } |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 613 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 614 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 615 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 616 | /// is something in the module with the specified name, return it potentially |
| 617 | /// bitcasted to the right type. |
| 618 | /// |
| 619 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 620 | /// to set the attributes on the global when it is first created. |
| 621 | llvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, |
| 622 | const llvm::PointerType*Ty, |
| 623 | const VarDecl *D) { |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 624 | // Lookup the entry, lazily creating it if necessary. |
Chris Lattner | 0b50625 | 2009-03-21 08:06:59 +0000 | [diff] [blame] | 625 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 626 | if (Entry) { |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 627 | if (Entry->getType() == Ty) |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 628 | return Entry; |
| 629 | |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 630 | // Make sure the result is of the correct type. |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 631 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 632 | } |
Chris Lattner | 38837f9 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 633 | |
| 634 | // This is the first use or definition of a mangled name. If there is a |
| 635 | // deferred decl with this name, remember that we need to emit it at the end |
| 636 | // of the file. |
| 637 | llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = |
| 638 | DeferredDecls.find(MangledName); |
| 639 | if (DDI != DeferredDecls.end()) { |
| 640 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 641 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 642 | DeferredDeclsToEmit.push_back(DDI->second); |
| 643 | DeferredDecls.erase(DDI); |
| 644 | } |
| 645 | |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 646 | llvm::GlobalVariable *GV = |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 647 | new llvm::GlobalVariable(Ty->getElementType(), false, |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 648 | llvm::GlobalValue::ExternalLinkage, |
Chris Lattner | 68306b3 | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 649 | 0, "", &getModule(), |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 650 | 0, Ty->getAddressSpace()); |
Chris Lattner | 68306b3 | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 651 | GV->setName(MangledName); |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 652 | |
| 653 | // Handle things which are present even on external declarations. |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 654 | if (D) { |
| 655 | // FIXME: This code is overly simple and should be merged with |
| 656 | // other global handling. |
| 657 | GV->setConstant(D->getType().isConstant(Context)); |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 658 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 659 | // FIXME: Merge with other attribute handling code. |
| 660 | if (D->getStorageClass() == VarDecl::PrivateExtern) |
| 661 | setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 662 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 663 | if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) |
| 664 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 665 | } |
| 666 | |
Chris Lattner | 7e5888f | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 667 | return Entry = GV; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Chris Lattner | aea1aee | 2009-03-22 21:03:39 +0000 | [diff] [blame^] | 670 | |
| 671 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 672 | /// given global variable. If Ty is non-null and if the global doesn't exist, |
| 673 | /// then it will be greated with the specified type instead of whatever the |
| 674 | /// normal requested type would be. |
| 675 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 676 | const llvm::Type *Ty) { |
| 677 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 678 | QualType ASTTy = D->getType(); |
| 679 | if (Ty == 0) |
| 680 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 681 | |
| 682 | const llvm::PointerType *PTy = |
| 683 | llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
| 684 | return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); |
| 685 | } |
| 686 | |
| 687 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 688 | /// specified type and name. |
| 689 | llvm::Constant * |
| 690 | CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, |
| 691 | const char *Name) { |
| 692 | // Convert Name to be a uniqued string from the IdentifierInfo table. |
| 693 | Name = getContext().Idents.get(Name).getName(); |
| 694 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); |
| 695 | } |
| 696 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 697 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 698 | llvm::Constant *Init = 0; |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 699 | QualType ASTTy = D->getType(); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 701 | if (D->getInit() == 0) { |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 702 | // This is a tentative definition; tentative definitions are |
| 703 | // implicitly initialized with { 0 } |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 704 | const llvm::Type *InitTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 705 | if (ASTTy->isIncompleteArrayType()) { |
| 706 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 707 | // to fix it to [ TYPE x 1 ]. |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 708 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(InitTy); |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 709 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 710 | } |
| 711 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 712 | } else { |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 713 | Init = EmitConstantExpr(D->getInit()); |
Eli Friedman | 442959a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 714 | if (!Init) { |
Daniel Dunbar | 3d78d3d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 715 | ErrorUnsupported(D, "static initializer"); |
Eli Friedman | 442959a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 716 | QualType T = D->getInit()->getType(); |
| 717 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 718 | } |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 719 | } |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 23afd5b | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 721 | const llvm::Type* InitType = Init->getType(); |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 722 | llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 723 | |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 724 | // Strip off a bitcast if we got one back. |
| 725 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 726 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 727 | Entry = CE->getOperand(0); |
| 728 | } |
| 729 | |
| 730 | // Entry is now either a Function or GlobalVariable. |
| 731 | llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
| 732 | |
| 733 | // If we already have this global and it has an initializer, then |
| 734 | // we are in the rare situation where we emitted the defining |
| 735 | // declaration of the global and are now being asked to emit a |
| 736 | // definition which would be common. This occurs, for example, in |
| 737 | // the following situation because statics can be emitted out of |
| 738 | // order: |
| 739 | // |
| 740 | // static int x; |
| 741 | // static int *y = &x; |
| 742 | // static int x = 10; |
| 743 | // int **z = &y; |
| 744 | // |
| 745 | // Bail here so we don't blow away the definition. Note that if we |
| 746 | // can't distinguish here if we emitted a definition with a null |
| 747 | // initializer, but this case is safe. |
| 748 | if (GV && GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { |
Daniel Dunbar | 3d78d3d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 749 | assert(!D->getInit() && "Emitting multiple definitions of a decl!"); |
| 750 | return; |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // We have a definition after a declaration with the wrong type. |
| 754 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 755 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 756 | // (which will be a definition). |
| 757 | // |
| 758 | // This happens if there is a prototype for a global (e.g. |
| 759 | // "extern int x[];") and then a definition of a different type (e.g. |
| 760 | // "int x[10];"). This also happens when an initializer has a different type |
| 761 | // from the type of the global (this happens with unions). |
| 762 | // |
| 763 | // FIXME: This also ends up happening if there's a definition followed by |
| 764 | // a tentative definition! (Although Sema rejects that construct |
| 765 | // at the moment.) |
| 766 | if (GV == 0 || |
| 767 | GV->getType()->getElementType() != InitType || |
| 768 | GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { |
| 769 | |
| 770 | // Remove the old entry from GlobalDeclMap so that we'll create a new one. |
| 771 | GlobalDeclMap.erase(getMangledName(D)); |
Daniel Dunbar | 3d78d3d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 772 | |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 773 | // Make a new global with the correct type, this is now guaranteed to work. |
| 774 | GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 775 | GV->takeName(cast<llvm::GlobalValue>(Entry)); |
| 776 | |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 777 | // Replace all uses of the old global with the new global |
| 778 | llvm::Constant *NewPtrForOldDecl = |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 779 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
| 780 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 781 | |
| 782 | // Erase the old global, since it is no longer used. |
Chris Lattner | 23afd5b | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 783 | // FIXME: What if it was attribute used? Dangling pointer from LLVMUsed. |
Chris Lattner | a698493 | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 784 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 785 | } |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 786 | |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 787 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 788 | SourceManager &SM = Context.getSourceManager(); |
| 789 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 790 | SM.getInstantiationLineNumber(D->getLocation()))); |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 793 | GV->setInitializer(Init); |
Nuno Lopes | a7bbf56 | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 794 | GV->setConstant(D->getType().isConstant(Context)); |
Eli Friedman | 175b73f | 2009-02-27 04:11:37 +0000 | [diff] [blame] | 795 | GV->setAlignment(getContext().getDeclAlignInBytes(D)); |
Eli Friedman | b232e99 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 797 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 798 | setGlobalVisibility(GV, attr->getVisibility()); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 799 | // FIXME: else handle -fvisibility |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 801 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 802 | if (D->getStorageClass() == VarDecl::Static) |
| 803 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 804 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 805 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 806 | else if (D->getAttr<DLLExportAttr>()) |
| 807 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Daniel Dunbar | 80f5f99 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 808 | else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) |
Mike Stump | 36dbf22 | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 809 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 810 | else { |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 811 | // FIXME: This isn't right. This should handle common linkage and other |
| 812 | // stuff. |
| 813 | switch (D->getStorageClass()) { |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 814 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 815 | case VarDecl::Auto: |
| 816 | case VarDecl::Register: |
| 817 | assert(0 && "Can't have auto or register globals"); |
| 818 | case VarDecl::None: |
| 819 | if (!D->getInit()) |
Duncan Sands | 78c33cc | 2009-03-11 20:15:27 +0000 | [diff] [blame] | 820 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Anders Carlsson | 689bba8 | 2008-12-03 05:51:23 +0000 | [diff] [blame] | 821 | else |
| 822 | GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 823 | break; |
| 824 | case VarDecl::Extern: |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 825 | // FIXME: common |
| 826 | break; |
| 827 | |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 828 | case VarDecl::PrivateExtern: |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 829 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 830 | // FIXME: common |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 831 | break; |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 832 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 833 | } |
Sanjiv Gupta | 54d9754 | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | 9750972 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 835 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 836 | GV->setSection(SA->getName()); |
| 837 | |
Daniel Dunbar | 375da3f | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 838 | if (D->getAttr<UsedAttr>()) |
| 839 | AddUsedGlobal(GV); |
| 840 | |
Sanjiv Gupta | 54d9754 | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 841 | // Emit global variable debug information. |
Chris Lattner | 23afd5b | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 842 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 6fc1f97 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 843 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 54d9754 | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 844 | DI->EmitGlobalVariable(GV, D); |
| 845 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 848 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 849 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 3f19784 | 2009-02-19 07:15:39 +0000 | [diff] [blame] | 850 | const llvm::FunctionType *Ty = |
| 851 | cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); |
| 852 | |
| 853 | // As a special case, make sure that definitions of K&R function |
| 854 | // "type foo()" aren't declared as varargs (which forces the backend |
| 855 | // to do unnecessary work). |
Chris Lattner | b7cee97 | 2009-03-22 19:35:37 +0000 | [diff] [blame] | 856 | if (D->getType()->isFunctionNoProtoType()) { |
| 857 | assert(Ty->isVarArg() && "Didn't lower type as expected"); |
| 858 | // Due to stret, the lowered function could have arguments. Just create the |
| 859 | // same type as was lowered by ConvertType but strip off the varargs bit. |
| 860 | std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); |
| 861 | Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); |
| 862 | } |
Daniel Dunbar | 3f19784 | 2009-02-19 07:15:39 +0000 | [diff] [blame] | 863 | |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 864 | // Get or create the prototype for teh function. |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 865 | llvm::Constant *Entry = GetAddrOfFunction(D, Ty); |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 866 | |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 867 | // Strip off a bitcast if we got one back. |
| 868 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 869 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 870 | Entry = CE->getOperand(0); |
| 871 | } |
| 872 | |
| 873 | |
| 874 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { |
Daniel Dunbar | edf953c | 2009-03-09 23:53:08 +0000 | [diff] [blame] | 875 | // If the types mismatch then we have to rewrite the definition. |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 876 | assert(cast<llvm::GlobalValue>(Entry)->isDeclaration() && |
| 877 | "Shouldn't replace non-declaration"); |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 878 | |
Chris Lattner | e28718c | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 879 | // F is the Function* for the one with the wrong type, we must make a new |
| 880 | // Function* and update everything that used F (a declaration) with the new |
| 881 | // Function* (which will be a definition). |
| 882 | // |
| 883 | // This happens if there is a prototype for a function |
| 884 | // (e.g. "int f()") and then a definition of a different type |
| 885 | // (e.g. "int f(int x)"). Start by making a new function of the |
| 886 | // correct type, RAUW, then steal the name. |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 887 | GlobalDeclMap.erase(getMangledName(D)); |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 888 | llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(D, Ty)); |
| 889 | NewFn->takeName(cast<llvm::GlobalValue>(Entry)); |
Chris Lattner | e28718c | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 890 | |
| 891 | // Replace uses of F with the Function we will endow with a body. |
| 892 | llvm::Constant *NewPtrForOldDecl = |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 893 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
| 894 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Chris Lattner | e28718c | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 895 | |
| 896 | // Ok, delete the old function now, which is dead. |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 897 | // FIXME: If it was attribute(used) the pointer will dangle from the |
| 898 | // LLVMUsed array! |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 899 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | e28718c | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 901 | Entry = NewFn; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 902 | } |
Chris Lattner | 5dd030f | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 903 | |
| 904 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 905 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 906 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 907 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 908 | SetFunctionAttributesForDefinition(D, Fn); |
| 909 | |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 910 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 911 | AddGlobalCtor(Fn, CA->getPriority()); |
Chris Lattner | d26784e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 912 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 913 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Chris Lattner | 9ec3ca2 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 916 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 917 | // Make sure that this type is translated. |
| 918 | Types.UpdateCompletedType(TD); |
Chris Lattner | 1b22f8b | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 922 | /// getBuiltinLibFunction |
Mike Stump | 96b7a15 | 2009-02-27 22:42:30 +0000 | [diff] [blame] | 923 | llvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 924 | if (BuiltinID > BuiltinFunctions.size()) |
| 925 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 926 | |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 927 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 928 | // a slot for it. |
| 929 | assert(BuiltinID && "Invalid Builtin ID"); |
Mike Stump | 96b7a15 | 2009-02-27 22:42:30 +0000 | [diff] [blame] | 930 | llvm::Value *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 931 | if (FunctionSlot) |
| 932 | return FunctionSlot; |
| 933 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 934 | assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || |
| 935 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && |
| 936 | "isn't a lib fn"); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 938 | // Get the name, skip over the __builtin_ prefix (if necessary). |
| 939 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID); |
| 940 | if (Context.BuiltinInfo.isLibFunction(BuiltinID)) |
| 941 | Name += 10; |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 942 | |
| 943 | // Get the type for the builtin. |
Douglas Gregor | 1fa246d | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 944 | Builtin::Context::GetBuiltinTypeError Error; |
| 945 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); |
| 946 | assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); |
| 947 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 948 | const llvm::FunctionType *Ty = |
| 949 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 950 | |
| 951 | // FIXME: This has a serious problem with code like this: |
| 952 | // void abs() {} |
| 953 | // ... __builtin_abs(x); |
| 954 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 955 | // and for the existing one to be turned into a constantexpr cast of the |
| 956 | // builtin. In the case where the existing one is a static function, it |
| 957 | // should just be renamed. |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 958 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 959 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 960 | return FunctionSlot = Existing; |
| 961 | assert(Existing == 0 && "FIXME: Name collision"); |
| 962 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 963 | |
Chris Lattner | 72cdb96 | 2009-03-01 18:47:06 +0000 | [diff] [blame] | 964 | llvm::GlobalValue *&ExistingFn = |
| 965 | GlobalDeclMap[getContext().Idents.get(Name).getName()]; |
Chris Lattner | 71fc581 | 2009-03-21 06:58:21 +0000 | [diff] [blame] | 966 | assert(!ExistingFn && "Asking for the same builtin multiple times?"); |
Mike Stump | 96b7a15 | 2009-02-27 22:42:30 +0000 | [diff] [blame] | 967 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 968 | // FIXME: param attributes for sext/zext etc. |
Chris Lattner | 72cdb96 | 2009-03-01 18:47:06 +0000 | [diff] [blame] | 969 | return FunctionSlot = ExistingFn = |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 970 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 971 | &getModule()); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 974 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 975 | unsigned NumTys) { |
| 976 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 977 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 978 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 979 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 980 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 981 | if (MemCpyFn) return MemCpyFn; |
Chris Lattner | e73302f | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 982 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 983 | return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 984 | } |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 985 | |
Eli Friedman | 8f08a25 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 986 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 987 | if (MemMoveFn) return MemMoveFn; |
Chris Lattner | e73302f | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 988 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 989 | return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); |
Eli Friedman | 8f08a25 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Lauro Ramos Venancio | e5bef73 | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 992 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 993 | if (MemSetFn) return MemSetFn; |
Chris Lattner | e73302f | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 994 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 995 | return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); |
Lauro Ramos Venancio | e5bef73 | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 996 | } |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 997 | |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 998 | static void appendFieldAndPadding(CodeGenModule &CGM, |
| 999 | std::vector<llvm::Constant*>& Fields, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1000 | FieldDecl *FieldD, FieldDecl *NextFieldD, |
| 1001 | llvm::Constant* Field, |
Chris Lattner | 08b05eb | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 1002 | RecordDecl* RD, const llvm::StructType *STy) { |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1003 | // Append the field. |
| 1004 | Fields.push_back(Field); |
| 1005 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1006 | int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1007 | |
| 1008 | int NextStructFieldNo; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1009 | if (!NextFieldD) { |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1010 | NextStructFieldNo = STy->getNumElements(); |
| 1011 | } else { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1012 | NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // Append padding |
| 1016 | for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { |
| 1017 | llvm::Constant *C = |
| 1018 | llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); |
| 1019 | |
| 1020 | Fields.push_back(C); |
| 1021 | } |
| 1022 | } |
| 1023 | |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1024 | // We still need to work out the details of handling UTF-16. |
| 1025 | // See: <rdr://2996215> |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1026 | llvm::Constant *CodeGenModule:: |
| 1027 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1028 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 1029 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 1030 | |
| 1031 | if (Entry.getValue()) |
| 1032 | return Entry.getValue(); |
| 1033 | |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1034 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 1035 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1036 | |
| 1037 | if (!CFConstantStringClassRef) { |
| 1038 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 1039 | Ty = llvm::ArrayType::get(Ty, 0); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1040 | |
| 1041 | // FIXME: This is fairly broken if |
| 1042 | // __CFConstantStringClassReference is already defined, in that it |
| 1043 | // will get renamed and the user will most likely see an opaque |
| 1044 | // error message. This is a general issue with relying on |
| 1045 | // particular names. |
| 1046 | llvm::GlobalVariable *GV = |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1047 | new llvm::GlobalVariable(Ty, false, |
| 1048 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 1049 | "__CFConstantStringClassReference", |
| 1050 | &getModule()); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1051 | |
| 1052 | // Decay array -> ptr |
| 1053 | CFConstantStringClassRef = |
| 1054 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1057 | QualType CFTy = getContext().getCFConstantStringType(); |
| 1058 | RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1059 | |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1060 | const llvm::StructType *STy = |
| 1061 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 1062 | |
| 1063 | std::vector<llvm::Constant*> Fields; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1064 | RecordDecl::field_iterator Field = CFRD->field_begin(); |
| 1065 | |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1066 | // Class pointer. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1067 | FieldDecl *CurField = *Field++; |
| 1068 | FieldDecl *NextField = *Field++; |
| 1069 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 1070 | CFConstantStringClassRef, CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1071 | |
| 1072 | // Flags. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1073 | CurField = NextField; |
| 1074 | NextField = *Field++; |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1075 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1076 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 1077 | llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1078 | |
| 1079 | // String pointer. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1080 | CurField = NextField; |
| 1081 | NextField = *Field++; |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1082 | llvm::Constant *C = llvm::ConstantArray::get(str); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1083 | C = new llvm::GlobalVariable(C->getType(), true, |
| 1084 | llvm::GlobalValue::InternalLinkage, |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1085 | C, ".str", &getModule()); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1086 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1087 | llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), |
| 1088 | CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1089 | |
| 1090 | // String length. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1091 | CurField = NextField; |
| 1092 | NextField = 0; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1093 | Ty = getTypes().ConvertType(getContext().LongTy); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1094 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 1095 | llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1096 | |
| 1097 | // The struct. |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1098 | C = llvm::ConstantStruct::get(STy, Fields); |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1099 | llvm::GlobalVariable *GV = |
| 1100 | new llvm::GlobalVariable(C->getType(), true, |
| 1101 | llvm::GlobalVariable::InternalLinkage, |
| 1102 | C, "", &getModule()); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1103 | |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1104 | GV->setSection("__DATA,__cfstring"); |
| 1105 | Entry.setValue(GV); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1106 | |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1107 | return GV; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1108 | } |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1109 | |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1110 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 3c670e1 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1111 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1112 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 3c670e1 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1113 | const char *StrData = E->getStrData(); |
| 1114 | unsigned Len = E->getByteLength(); |
| 1115 | |
| 1116 | const ConstantArrayType *CAT = |
| 1117 | getContext().getAsConstantArrayType(E->getType()); |
| 1118 | assert(CAT && "String isn't pointer or array!"); |
| 1119 | |
Chris Lattner | 1403222 | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1120 | // Resize the string to the right size. |
Daniel Dunbar | 3c670e1 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1121 | std::string Str(StrData, StrData+Len); |
| 1122 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
Chris Lattner | 1403222 | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (E->isWide()) |
| 1125 | RealLen *= getContext().Target.getWCharWidth()/8; |
| 1126 | |
Daniel Dunbar | 3c670e1 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1127 | Str.resize(RealLen, '\0'); |
| 1128 | |
| 1129 | return Str; |
| 1130 | } |
| 1131 | |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1132 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 1133 | /// constant array for the given string literal. |
| 1134 | llvm::Constant * |
| 1135 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 1136 | // FIXME: This can be more efficient. |
| 1137 | return GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 1138 | } |
| 1139 | |
Chris Lattner | c5d3263 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1140 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 1141 | /// array for the given ObjCEncodeExpr node. |
| 1142 | llvm::Constant * |
| 1143 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 1144 | std::string Str; |
| 1145 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | dc8d1c6 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 1146 | |
| 1147 | return GetAddrOfConstantCString(Str); |
Chris Lattner | c5d3263 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1151 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1152 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 1153 | bool constant, |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1154 | CodeGenModule &CGM, |
| 1155 | const char *GlobalName) { |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1156 | // Create Constant for this string literal. Don't add a '\0'. |
| 1157 | llvm::Constant *C = llvm::ConstantArray::get(str, false); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Create a global variable for this string |
Chris Lattner | c5d3263 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1160 | return new llvm::GlobalVariable(C->getType(), constant, |
| 1161 | llvm::GlobalValue::InternalLinkage, |
| 1162 | C, GlobalName ? GlobalName : ".str", |
| 1163 | &CGM.getModule()); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1166 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 1167 | /// containing the literal. This contents are exactly that of the |
| 1168 | /// given string, i.e. it will not be null terminated automatically; |
| 1169 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 1170 | /// actually a pointer to an LLVM constant depends on |
| 1171 | /// Feature.WriteableStrings. |
| 1172 | /// |
| 1173 | /// The result has pointer to array type. |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1174 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, |
| 1175 | const char *GlobalName) { |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1176 | // Don't share any string literals if writable-strings is turned on. |
| 1177 | if (Features.WritableStrings) |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1178 | return GenerateStringLiteral(str, false, *this, GlobalName); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1179 | |
| 1180 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 1181 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 1182 | |
| 1183 | if (Entry.getValue()) |
Chris Lattner | c5d3263 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1184 | return Entry.getValue(); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Create a global variable for this. |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1187 | llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1188 | Entry.setValue(C); |
| 1189 | return C; |
| 1190 | } |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1191 | |
| 1192 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 1193 | /// array containing the literal and a terminating '\-' |
| 1194 | /// character. The result has pointer to array type. |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1195 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, |
| 1196 | const char *GlobalName){ |
Chris Lattner | b217601 | 2008-12-09 19:10:54 +0000 | [diff] [blame] | 1197 | return GetAddrOfConstantString(str + '\0', GlobalName); |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1198 | } |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1199 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1200 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 1201 | /// properties for an implementation. |
| 1202 | void CodeGenModule::EmitObjCPropertyImplementations(const |
| 1203 | ObjCImplementationDecl *D) { |
| 1204 | for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), |
| 1205 | e = D->propimpl_end(); i != e; ++i) { |
| 1206 | ObjCPropertyImplDecl *PID = *i; |
| 1207 | |
| 1208 | // Dynamic is just for type-checking. |
| 1209 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1210 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1211 | |
| 1212 | // Determine which methods need to be implemented, some may have |
| 1213 | // been overridden. Note that ::isSynthesized is not the method |
| 1214 | // we want, that just indicates if the decl came from a |
| 1215 | // property. What we want to know is if the method is defined in |
| 1216 | // this implementation. |
| 1217 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 91dd9d3 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1218 | CodeGenFunction(*this).GenerateObjCGetter( |
| 1219 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1220 | if (!PD->isReadOnly() && |
| 1221 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 91dd9d3 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1222 | CodeGenFunction(*this).GenerateObjCSetter( |
| 1223 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1228 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 1229 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 1230 | // If an error has occurred, stop code generation, but continue |
| 1231 | // parsing and semantic analysis (to ensure all warnings and errors |
| 1232 | // are emitted). |
| 1233 | if (Diags.hasErrorOccurred()) |
| 1234 | return; |
| 1235 | |
| 1236 | switch (D->getKind()) { |
| 1237 | case Decl::Function: |
| 1238 | case Decl::Var: |
| 1239 | EmitGlobal(cast<ValueDecl>(D)); |
| 1240 | break; |
| 1241 | |
| 1242 | case Decl::Namespace: |
Daniel Dunbar | 952f473 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 1243 | ErrorUnsupported(D, "namespace"); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1244 | break; |
| 1245 | |
| 1246 | // Objective-C Decls |
| 1247 | |
Fariborz Jahanian | 2c7de6d | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1248 | // Forward declarations, no (immediate) code generation. |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1249 | case Decl::ObjCClass: |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1250 | case Decl::ObjCForwardProtocol: |
Fariborz Jahanian | ca27732 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1251 | case Decl::ObjCCategory: |
| 1252 | case Decl::ObjCInterface: |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1253 | break; |
Fariborz Jahanian | 2c7de6d | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1254 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1255 | case Decl::ObjCProtocol: |
Fariborz Jahanian | ca27732 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1256 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1257 | break; |
| 1258 | |
| 1259 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1260 | // Categories have properties but don't support synthesize so we |
| 1261 | // can ignore them here. |
| 1262 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1263 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 1264 | break; |
| 1265 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1266 | case Decl::ObjCImplementation: { |
| 1267 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 1268 | EmitObjCPropertyImplementations(OMD); |
| 1269 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1270 | break; |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1271 | } |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1272 | case Decl::ObjCMethod: { |
| 1273 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 1274 | // If this is not a prototype, emit the body. |
| 1275 | if (OMD->getBody()) |
| 1276 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 1277 | break; |
| 1278 | } |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1279 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 2ac4cd3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1280 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | |
| 1283 | case Decl::LinkageSpec: { |
| 1284 | LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); |
| 1285 | if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 1286 | ErrorUnsupported(LSD, "linkage spec"); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1287 | // FIXME: implement C++ linkage, C linkage works mostly by C |
| 1288 | // language reuse already. |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | case Decl::FileScopeAsm: { |
| 1293 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
| 1294 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 1295 | AD->getAsmString()->getByteLength()); |
| 1296 | |
| 1297 | const std::string &S = getModule().getModuleInlineAsm(); |
| 1298 | if (S.empty()) |
| 1299 | getModule().setModuleInlineAsm(AsmString); |
| 1300 | else |
| 1301 | getModule().setModuleInlineAsm(S + '\n' + AsmString); |
| 1302 | break; |
| 1303 | } |
| 1304 | |
| 1305 | default: |
| 1306 | // Make sure we handled everything we should, every other kind is |
| 1307 | // a non-top-level decl. FIXME: Would be nice to have an |
| 1308 | // isTopLevelDeclKind function. Need to recode Decl::Kind to do |
| 1309 | // that easily. |
| 1310 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 1311 | } |
| 1312 | } |