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