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