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