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