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