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