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