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