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