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