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" |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Decl.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 20 | #include "clang/Basic/LangOptions.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "llvm/DerivedTypes.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" |
Chris Lattner | a1945fa | 2008-04-30 16:05:42 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/Verifier.h" |
Christopher Lamb | ce39faa | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
| 34 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 35 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 36 | llvm::Module &M, const llvm::TargetData &TD, |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 37 | Diagnostic &diags, bool GenerateDebugInfo) |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 38 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 39 | Types(C, M, TD), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), |
| 40 | CFConstantStringClassRef(0) { |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 41 | //TODO: Make this selectable at runtime |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 42 | Runtime = CreateObjCRuntime(*this); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 43 | |
| 44 | // If debug info generation is enabled, create the CGDebugInfo object. |
| 45 | if (GenerateDebugInfo) |
| 46 | DebugInfo = new CGDebugInfo(this); |
| 47 | else |
| 48 | DebugInfo = NULL; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CodeGenModule::~CodeGenModule() { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 52 | EmitStatics(); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 53 | llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction(); |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 54 | if (ObjCInitFunction) |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 55 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 56 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 57 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 58 | EmitAnnotations(); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 59 | delete Runtime; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 60 | delete DebugInfo; |
Chris Lattner | a1945fa | 2008-04-30 16:05:42 +0000 | [diff] [blame] | 61 | // Run the verifier to check that the generated code is consistent. |
| 62 | assert(!verifyModule(TheModule)); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 63 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 65 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 66 | /// specified stmt yet. |
| 67 | void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) { |
| 68 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 69 | "cannot codegen this %0 yet"); |
| 70 | SourceRange Range = S->getSourceRange(); |
| 71 | std::string Msg = Type; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 72 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID, |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 73 | &Msg, 1, &Range, 1); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 75 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 76 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 77 | /// specified decl yet. |
| 78 | void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) { |
| 79 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 80 | "cannot codegen this %0 yet"); |
| 81 | std::string Msg = Type; |
| 82 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID, |
| 83 | &Msg, 1); |
| 84 | } |
| 85 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 86 | /// setVisibility - Set the visibility for the given LLVM GlobalValue |
| 87 | /// according to the given clang AST visibility value. |
| 88 | void CodeGenModule::setVisibility(llvm::GlobalValue *GV, |
| 89 | VisibilityAttr::VisibilityTypes Vis) { |
| 90 | switch (Vis) { |
| 91 | default: assert(0 && "Unknown visibility!"); |
| 92 | case VisibilityAttr::DefaultVisibility: |
| 93 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 94 | break; |
| 95 | case VisibilityAttr::HiddenVisibility: |
| 96 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 97 | break; |
| 98 | case VisibilityAttr::ProtectedVisibility: |
| 99 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 104 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 105 | /// main() runs. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 106 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 107 | // TODO: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 108 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 111 | /// AddGlobalDtor - Add a function to the list that will be called |
| 112 | /// when the module is unloaded. |
| 113 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
| 114 | // TODO: Type coercion of void()* types. |
| 115 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 116 | } |
| 117 | |
| 118 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 119 | // Ctor function type is void()*. |
| 120 | llvm::FunctionType* CtorFTy = |
| 121 | llvm::FunctionType::get(llvm::Type::VoidTy, |
| 122 | std::vector<const llvm::Type*>(), |
| 123 | false); |
| 124 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
| 125 | |
| 126 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 127 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 128 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 129 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 131 | // Construct the constructor and destructor arrays. |
| 132 | std::vector<llvm::Constant*> Ctors; |
| 133 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 134 | std::vector<llvm::Constant*> S; |
| 135 | S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); |
| 136 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
| 137 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 138 | } |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 139 | |
| 140 | if (!Ctors.empty()) { |
| 141 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
| 142 | new llvm::GlobalVariable(AT, false, |
| 143 | llvm::GlobalValue::AppendingLinkage, |
| 144 | llvm::ConstantArray::get(AT, Ctors), |
| 145 | GlobalName, |
| 146 | &TheModule); |
| 147 | } |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 150 | void CodeGenModule::EmitAnnotations() { |
| 151 | if (Annotations.empty()) |
| 152 | return; |
| 153 | |
| 154 | // Create a new global variable for the ConstantStruct in the Module. |
| 155 | llvm::Constant *Array = |
| 156 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 157 | Annotations.size()), |
| 158 | Annotations); |
| 159 | llvm::GlobalValue *gv = |
| 160 | new llvm::GlobalVariable(Array->getType(), false, |
| 161 | llvm::GlobalValue::AppendingLinkage, Array, |
| 162 | "llvm.global.annotations", &TheModule); |
| 163 | gv->setSection("llvm.metadata"); |
| 164 | } |
| 165 | |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 166 | bool hasAggregateLLVMType(QualType T) { |
| 167 | return !T->isRealType() && !T->isPointerLikeType() && |
| 168 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
| 169 | } |
| 170 | |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 171 | void CodeGenModule::SetGlobalValueAttributes(const FunctionDecl *FD, |
| 172 | llvm::GlobalValue *GV) { |
| 173 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 174 | // approximation of what we really want. |
| 175 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 176 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 177 | else if (FD->getAttr<DLLImportAttr>()) |
| 178 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 179 | else if (FD->getAttr<DLLExportAttr>()) |
| 180 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 181 | else if (FD->getAttr<WeakAttr>() || FD->isInline()) |
| 182 | GV->setLinkage(llvm::Function::WeakLinkage); |
| 183 | |
| 184 | if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>()) |
| 185 | CodeGenModule::setVisibility(GV, attr->getVisibility()); |
| 186 | // FIXME: else handle -fvisibility |
| 187 | } |
| 188 | |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 189 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 190 | llvm::Function *F, |
| 191 | const llvm::FunctionType *FTy) { |
| 192 | unsigned FuncAttrs = 0; |
| 193 | if (FD->getAttr<NoThrowAttr>()) |
| 194 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
| 195 | if (FD->getAttr<NoReturnAttr>()) |
| 196 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 197 | |
| 198 | llvm::SmallVector<llvm::ParamAttrsWithIndex, 8> ParamAttrList; |
| 199 | if (FuncAttrs) |
| 200 | ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); |
| 201 | // Note that there is parallel code in CodeGenFunction::EmitCallExpr |
| 202 | bool AggregateReturn = hasAggregateLLVMType(FD->getResultType()); |
| 203 | if (AggregateReturn) |
| 204 | ParamAttrList.push_back( |
| 205 | llvm::ParamAttrsWithIndex::get(1, llvm::ParamAttr::StructRet)); |
| 206 | unsigned increment = AggregateReturn ? 2 : 1; |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 207 | const FunctionTypeProto* FTP = dyn_cast<FunctionTypeProto>(FD->getType()); |
| 208 | if (FTP) { |
| 209 | for (unsigned i = 0; i < FTP->getNumArgs(); i++) { |
| 210 | QualType ParamType = FTP->getArgType(i); |
| 211 | unsigned ParamAttrs = 0; |
| 212 | if (ParamType->isRecordType()) |
| 213 | ParamAttrs |= llvm::ParamAttr::ByVal; |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 214 | if (ParamType->isSignedIntegerType() && |
| 215 | ParamType->isPromotableIntegerType()) |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 216 | ParamAttrs |= llvm::ParamAttr::SExt; |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 217 | if (ParamType->isUnsignedIntegerType() && |
| 218 | ParamType->isPromotableIntegerType()) |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 219 | ParamAttrs |= llvm::ParamAttr::ZExt; |
| 220 | if (ParamAttrs) |
| 221 | ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(i + increment, |
| 222 | ParamAttrs)); |
| 223 | } |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 224 | } |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 225 | |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 226 | F->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), |
| 227 | ParamAttrList.size())); |
| 228 | |
| 229 | // Set the appropriate calling convention for the Function. |
| 230 | if (FD->getAttr<FastCallAttr>()) |
| 231 | F->setCallingConv(llvm::CallingConv::Fast); |
| 232 | |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 233 | SetGlobalValueAttributes(FD, F); |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 236 | void CodeGenModule::EmitObjCMethod(const ObjCMethodDecl *OMD) { |
| 237 | // If this is not a prototype, emit the body. |
| 238 | if (OMD->getBody()) |
| 239 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 240 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 241 | void CodeGenModule::EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD){ |
| 242 | llvm::SmallVector<std::string, 16> Protocols; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 243 | for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), |
| 244 | E = PD->protocol_end(); PI != E; ++PI) |
| 245 | Protocols.push_back((*PI)->getName()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 246 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 247 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 248 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 249 | E = PD->instmeth_end(); iter != E; iter++) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 250 | std::string TypeStr; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 251 | Context.getObjCEncodingForMethodDecl(*iter, TypeStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 252 | InstanceMethodNames.push_back( |
| 253 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 254 | InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 255 | } |
| 256 | // Collect information about class methods: |
| 257 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 258 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 259 | for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(), |
| 260 | endIter = PD->classmeth_end() ; iter != endIter ; iter++) { |
| 261 | std::string TypeStr; |
| 262 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 263 | ClassMethodNames.push_back( |
| 264 | GetAddrOfConstantString((*iter)->getSelector().getName())); |
| 265 | ClassMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 266 | } |
| 267 | Runtime->GenerateProtocol(PD->getName(), Protocols, InstanceMethodNames, |
| 268 | InstanceMethodTypes, ClassMethodNames, ClassMethodTypes); |
| 269 | } |
| 270 | |
| 271 | void CodeGenModule::EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD) { |
| 272 | |
| 273 | // Collect information about instance methods |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 274 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 275 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 276 | for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(), |
| 277 | endIter = OCD->instmeth_end() ; iter != endIter ; iter++) { |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 278 | InstanceMethodSels.push_back((*iter)->getSelector()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 279 | std::string TypeStr; |
Chris Lattner | fba6763 | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 280 | Context.getObjCEncodingForMethodDecl(*iter,TypeStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 281 | InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 282 | } |
| 283 | |
| 284 | // Collect information about class methods |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 285 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 286 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 287 | for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(), |
| 288 | endIter = OCD->classmeth_end() ; iter != endIter ; iter++) { |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 289 | ClassMethodSels.push_back((*iter)->getSelector()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 290 | std::string TypeStr; |
Chris Lattner | fba6763 | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 291 | Context.getObjCEncodingForMethodDecl(*iter,TypeStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 292 | ClassMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 293 | } |
| 294 | |
| 295 | // Collect the names of referenced protocols |
| 296 | llvm::SmallVector<std::string, 16> Protocols; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 297 | const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface(); |
| 298 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 299 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 300 | E = Protos.end(); I != E; ++I) |
| 301 | Protocols.push_back((*I)->getName()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 302 | |
| 303 | // Generate the category |
| 304 | Runtime->GenerateCategory(OCD->getClassInterface()->getName(), |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 305 | OCD->getName(), InstanceMethodSels, InstanceMethodTypes, |
| 306 | ClassMethodSels, ClassMethodTypes, Protocols); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void CodeGenModule::EmitObjCClassImplementation( |
| 310 | const ObjCImplementationDecl *OID) { |
| 311 | // Get the superclass name. |
| 312 | const ObjCInterfaceDecl * SCDecl = OID->getClassInterface()->getSuperClass(); |
| 313 | const char * SCName = NULL; |
| 314 | if (SCDecl) { |
| 315 | SCName = SCDecl->getName(); |
| 316 | } |
| 317 | |
| 318 | // Get the class name |
| 319 | ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface(); |
| 320 | const char * ClassName = ClassDecl->getName(); |
| 321 | |
| 322 | // Get the size of instances. For runtimes that support late-bound instances |
| 323 | // this should probably be something different (size just of instance |
| 324 | // varaibles in this class, not superclasses?). |
| 325 | int instanceSize = 0; |
| 326 | const llvm::Type *ObjTy; |
| 327 | if (!Runtime->LateBoundIVars()) { |
| 328 | ObjTy = getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl)); |
| 329 | instanceSize = TheTargetData.getABITypeSize(ObjTy); |
| 330 | } |
| 331 | |
| 332 | // Collect information about instance variables. |
| 333 | llvm::SmallVector<llvm::Constant*, 16> IvarNames; |
| 334 | llvm::SmallVector<llvm::Constant*, 16> IvarTypes; |
| 335 | llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; |
| 336 | const llvm::StructLayout *Layout = |
| 337 | TheTargetData.getStructLayout(cast<llvm::StructType>(ObjTy)); |
| 338 | ObjTy = llvm::PointerType::getUnqual(ObjTy); |
| 339 | for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), |
| 340 | endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { |
| 341 | // Store the name |
| 342 | IvarNames.push_back(GetAddrOfConstantString((*iter)->getName())); |
| 343 | // Get the type encoding for this ivar |
| 344 | std::string TypeStr; |
| 345 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
| 346 | Context.getObjCEncodingForType((*iter)->getType(), TypeStr, |
| 347 | EncodingRecordTypes); |
| 348 | IvarTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 349 | // Get the offset |
| 350 | int offset = |
| 351 | (int)Layout->getElementOffset(getTypes().getLLVMFieldNo(*iter)); |
| 352 | IvarOffsets.push_back( |
| 353 | llvm::ConstantInt::get(llvm::Type::Int32Ty, offset)); |
| 354 | } |
| 355 | |
| 356 | // Collect information about instance methods |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 357 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 358 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 359 | for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(), |
| 360 | endIter = OID->instmeth_end() ; iter != endIter ; iter++) { |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 361 | InstanceMethodSels.push_back((*iter)->getSelector()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 362 | std::string TypeStr; |
| 363 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 364 | InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 365 | } |
| 366 | |
| 367 | // Collect information about class methods |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 368 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 369 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 370 | for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(), |
| 371 | endIter = OID->classmeth_end() ; iter != endIter ; iter++) { |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 372 | ClassMethodSels.push_back((*iter)->getSelector()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 373 | std::string TypeStr; |
| 374 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 375 | ClassMethodTypes.push_back(GetAddrOfConstantString(TypeStr)); |
| 376 | } |
| 377 | // Collect the names of referenced protocols |
| 378 | llvm::SmallVector<std::string, 16> Protocols; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 379 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 380 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 381 | E = Protos.end(); I != E; ++I) |
| 382 | Protocols.push_back((*I)->getName()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 383 | |
| 384 | // Generate the category |
| 385 | Runtime->GenerateClass(ClassName, SCName, instanceSize, IvarNames, IvarTypes, |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 386 | IvarOffsets, InstanceMethodSels, InstanceMethodTypes, |
| 387 | ClassMethodSels, ClassMethodTypes, Protocols); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 390 | void CodeGenModule::EmitStatics() { |
| 391 | // Emit code for each used static decl encountered. Since a previously unused |
| 392 | // static decl may become used during the generation of code for a static |
| 393 | // function, iterate until no changes are made. |
| 394 | bool Changed; |
| 395 | do { |
| 396 | Changed = false; |
| 397 | for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 398 | const ValueDecl *D = StaticDecls[i]; |
Eli Friedman | 6f7e2ee | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 399 | |
| 400 | // Check if we have used a decl with the same name |
| 401 | // FIXME: The AST should have some sort of aggregate decls or |
| 402 | // global symbol map. |
| 403 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 404 | if (!getModule().getFunction(FD->getName())) |
| 405 | continue; |
| 406 | } else { |
| 407 | if (!getModule().getNamedGlobal(cast<VarDecl>(D)->getName())) |
| 408 | continue; |
| 409 | } |
| 410 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 411 | // Emit the definition. |
| 412 | EmitGlobalDefinition(D); |
| 413 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 414 | // Erase the used decl from the list. |
| 415 | StaticDecls[i] = StaticDecls.back(); |
| 416 | StaticDecls.pop_back(); |
| 417 | --i; |
| 418 | --e; |
| 419 | |
| 420 | // Remember that we made a change. |
| 421 | Changed = true; |
| 422 | } |
| 423 | } while (Changed); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 426 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 427 | /// annotation information for a given GlobalValue. The annotation struct is |
| 428 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
| 429 | /// GlobalValue being annotated. The second filed is thee constant string |
| 430 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 431 | /// string containing the name of the translation unit. The fourth field is |
| 432 | /// the line number in the file of the annotated value declaration. |
| 433 | /// |
| 434 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 435 | /// appears to. |
| 436 | /// |
| 437 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 438 | const AnnotateAttr *AA, |
| 439 | unsigned LineNo) { |
| 440 | llvm::Module *M = &getModule(); |
| 441 | |
| 442 | // get [N x i8] constants for the annotation string, and the filename string |
| 443 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 444 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 445 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 446 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 447 | true); |
| 448 | |
| 449 | // Get the two global values corresponding to the ConstantArrays we just |
| 450 | // created to hold the bytes of the strings. |
| 451 | llvm::GlobalValue *annoGV = |
| 452 | new llvm::GlobalVariable(anno->getType(), false, |
| 453 | llvm::GlobalValue::InternalLinkage, anno, |
| 454 | GV->getName() + ".str", M); |
| 455 | // translation unit name string, emitted into the llvm.metadata section. |
| 456 | llvm::GlobalValue *unitGV = |
| 457 | new llvm::GlobalVariable(unit->getType(), false, |
| 458 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 459 | |
| 460 | // Create the ConstantStruct that is the global annotion. |
| 461 | llvm::Constant *Fields[4] = { |
| 462 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 463 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 464 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 465 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 466 | }; |
| 467 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 468 | } |
| 469 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 470 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 471 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 472 | /// them to point to NewVal. This is badbadbad, FIXME! |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 473 | void CodeGenModule::ReplaceMapValuesWith(llvm::GlobalValue *OldVal, |
| 474 | llvm::GlobalValue *NewVal) { |
| 475 | for (llvm::DenseMap<const Decl*, llvm::GlobalValue*>::iterator |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 476 | I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I) |
| 477 | if (I->second == OldVal) I->second = NewVal; |
| 478 | } |
| 479 | |
| 480 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
| 481 | bool isDef, isStatic; |
| 482 | |
| 483 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
| 484 | isDef = (FD->isThisDeclarationADefinition() || |
| 485 | FD->getAttr<AliasAttr>()); |
| 486 | isStatic = FD->getStorageClass() == FunctionDecl::Static; |
| 487 | } else if (const VarDecl *VD = cast<VarDecl>(Global)) { |
| 488 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 489 | |
| 490 | isDef = !(VD->getStorageClass() == VarDecl::Extern && VD->getInit() == 0); |
| 491 | isStatic = VD->getStorageClass() == VarDecl::Static; |
| 492 | } else { |
| 493 | assert(0 && "Invalid argument to EmitGlobal"); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 497 | // Forward declarations are emitted lazily on first use. |
| 498 | if (!isDef) |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 499 | return; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 500 | |
| 501 | // If the global is a static, defer code generation until later so |
| 502 | // we can easily omit unused statics. |
| 503 | if (isStatic) { |
| 504 | StaticDecls.push_back(Global); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | // Otherwise emit the definition. |
| 509 | EmitGlobalDefinition(Global); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 512 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 513 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 514 | EmitGlobalFunctionDefinition(FD); |
| 515 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 516 | EmitGlobalVarDefinition(VD); |
| 517 | } else { |
| 518 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 519 | } |
| 520 | } |
| 521 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 522 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 523 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 524 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 525 | QualType ASTTy = D->getType(); |
| 526 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 527 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 528 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 529 | // See if it is already in the map. |
| 530 | llvm::GlobalValue *&Entry = GlobalDeclMap[D]; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 531 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 532 | // If not look for an existing global (if this decl shadows another |
| 533 | // one) or lazily create a forward declaration. |
| 534 | if (!Entry) { |
| 535 | // Check to see if the global already exists. |
| 536 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true); |
| 537 | |
| 538 | // Create it if not. |
| 539 | if (!GV) |
| 540 | GV = new llvm::GlobalVariable(Ty, false, |
| 541 | llvm::GlobalValue::ExternalLinkage, |
| 542 | 0, D->getName(), &getModule(), 0, |
| 543 | ASTTy.getAddressSpace()); |
| 544 | |
| 545 | // Cache the entry. |
| 546 | Entry = GV; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 549 | // Make sure the result is of the correct type. |
| 550 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 554 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 555 | QualType ASTTy = D->getType(); |
| 556 | const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 558 | if (D->getInit() == 0) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 559 | // This is a tentative definition; tentative definitions are |
| 560 | // implicitly initialized with { 0 } |
| 561 | const llvm::Type* InitTy; |
| 562 | if (ASTTy->isIncompleteArrayType()) { |
| 563 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 564 | // to fix it to [ TYPE x 1 ]. |
| 565 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); |
| 566 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
| 567 | } else { |
| 568 | InitTy = VarTy; |
| 569 | } |
| 570 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 571 | } else { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 572 | Init = EmitConstantExpr(D->getInit()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 573 | } |
| 574 | const llvm::Type* InitType = Init->getType(); |
| 575 | |
| 576 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName(), true); |
| 577 | |
| 578 | if (!GV) { |
| 579 | GV = new llvm::GlobalVariable(InitType, false, |
| 580 | llvm::GlobalValue::ExternalLinkage, |
| 581 | 0, D->getName(), &getModule(), 0, |
| 582 | ASTTy.getAddressSpace()); |
| 583 | } else if (GV->getType()->getElementType() != InitType || |
| 584 | GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { |
| 585 | // We have a definition after a prototype with the wrong type. |
| 586 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 587 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 588 | // (which will be a definition). |
| 589 | // |
| 590 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 591 | // and then a definition of a different type (e.g. "int x[10];"). This also |
| 592 | // happens when an initializer has a different type from the type of the |
| 593 | // global (this happens with unions). |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 594 | // |
| 595 | // FIXME: This also ends up happening if there's a definition followed by |
| 596 | // a tentative definition! (Although Sema rejects that construct |
| 597 | // at the moment.) |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 598 | |
| 599 | // Save the old global |
| 600 | llvm::GlobalVariable *OldGV = GV; |
| 601 | |
| 602 | // Make a new global with the correct type |
| 603 | GV = new llvm::GlobalVariable(InitType, false, |
| 604 | llvm::GlobalValue::ExternalLinkage, |
| 605 | 0, D->getName(), &getModule(), 0, |
| 606 | ASTTy.getAddressSpace()); |
| 607 | // Steal the name of the old global |
| 608 | GV->takeName(OldGV); |
| 609 | |
| 610 | // Replace all uses of the old global with the new global |
| 611 | llvm::Constant *NewPtrForOldDecl = |
| 612 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 613 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 614 | // Make sure we don't keep around any stale references to globals |
| 615 | // FIXME: This is really slow; we need a better way to walk all |
| 616 | // the decls with the same name |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 617 | ReplaceMapValuesWith(OldGV, GV); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 618 | |
| 619 | // Erase the old global, since it is no longer used. |
| 620 | OldGV->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 621 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 622 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 623 | GlobalDeclMap[D] = GV; |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 624 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 625 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 626 | SourceManager &SM = Context.getSourceManager(); |
| 627 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
| 628 | SM.getLogicalLineNumber(D->getLocation()))); |
| 629 | } |
| 630 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 631 | GV->setInitializer(Init); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 632 | |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 633 | // FIXME: This is silly; getTypeAlign should just work for incomplete arrays |
| 634 | unsigned Align; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 635 | if (const IncompleteArrayType* IAT = |
| 636 | Context.getAsIncompleteArrayType(D->getType())) |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 637 | Align = Context.getTypeAlign(IAT->getElementType()); |
| 638 | else |
| 639 | Align = Context.getTypeAlign(D->getType()); |
Eli Friedman | 08d7802 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 640 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { |
| 641 | Align = std::max(Align, AA->getAlignment()); |
| 642 | } |
| 643 | GV->setAlignment(Align / 8); |
| 644 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 645 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 646 | setVisibility(GV, attr->getVisibility()); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 647 | // FIXME: else handle -fvisibility |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 648 | |
| 649 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 650 | if (D->getStorageClass() == VarDecl::Static) |
| 651 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 652 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 653 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 654 | else if (D->getAttr<DLLExportAttr>()) |
| 655 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 656 | else if (D->getAttr<WeakAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 657 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 658 | else { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 659 | // FIXME: This isn't right. This should handle common linkage and other |
| 660 | // stuff. |
| 661 | switch (D->getStorageClass()) { |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 662 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 663 | case VarDecl::Auto: |
| 664 | case VarDecl::Register: |
| 665 | assert(0 && "Can't have auto or register globals"); |
| 666 | case VarDecl::None: |
| 667 | if (!D->getInit()) |
Eli Friedman | a07b764 | 2008-05-29 11:03:17 +0000 | [diff] [blame] | 668 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 669 | break; |
| 670 | case VarDecl::Extern: |
| 671 | case VarDecl::PrivateExtern: |
| 672 | // todo: common |
| 673 | break; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 674 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 675 | } |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 676 | |
| 677 | // Emit global variable debug information. |
| 678 | CGDebugInfo *DI = getDebugInfo(); |
| 679 | if(DI) { |
| 680 | if(D->getLocation().isValid()) |
| 681 | DI->setLocation(D->getLocation()); |
| 682 | DI->EmitGlobalVariable(GV, D); |
| 683 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 684 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 685 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 686 | llvm::GlobalValue * |
| 687 | CodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { |
| 688 | // FIXME: param attributes for sext/zext etc. |
| 689 | if (const AliasAttr *AA = D->getAttr<AliasAttr>()) { |
| 690 | assert(!D->getBody() && "Unexpected alias attr on function with body."); |
| 691 | |
| 692 | const std::string& aliaseeName = AA->getAliasee(); |
| 693 | llvm::Function *aliasee = getModule().getFunction(aliaseeName); |
| 694 | llvm::GlobalValue *alias = new llvm::GlobalAlias(aliasee->getType(), |
| 695 | llvm::Function::ExternalLinkage, |
| 696 | D->getName(), |
| 697 | aliasee, |
| 698 | &getModule()); |
| 699 | SetGlobalValueAttributes(D, alias); |
| 700 | return alias; |
| 701 | } else { |
| 702 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 703 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 704 | llvm::Function *F = llvm::Function::Create(FTy, |
| 705 | llvm::Function::ExternalLinkage, |
| 706 | D->getName(), &getModule()); |
| 707 | |
| 708 | SetFunctionAttributes(D, F, FTy); |
| 709 | return F; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 714 | QualType ASTTy = D->getType(); |
| 715 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 716 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
| 717 | |
| 718 | // See if it is already in the map. |
| 719 | llvm::GlobalValue *&Entry = GlobalDeclMap[D]; |
| 720 | |
| 721 | // If not look for an existing global (if this decl shadows another |
| 722 | // one) or lazily create a forward declaration. |
| 723 | if (!Entry) { |
| 724 | // Check to see if the global already exists. |
| 725 | llvm::GlobalValue *GV = getModule().getFunction(D->getName()); |
| 726 | |
| 727 | // Create it if not. |
| 728 | if (!GV) |
| 729 | GV = EmitForwardFunctionDefinition(D); |
| 730 | |
| 731 | // Cache the entry. |
| 732 | Entry = GV; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 735 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 739 | llvm::GlobalValue *&Entry = GlobalDeclMap[D]; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 740 | |
| 741 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 742 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 743 | |
| 744 | // Check to see if the function already exists. |
| 745 | llvm::Function *F = getModule().getFunction(D->getName()); |
| 746 | |
| 747 | // If it doesn't already exist, just create and return an entry. |
| 748 | if (F == 0) { |
| 749 | Entry = EmitForwardFunctionDefinition(D); |
| 750 | } else { |
| 751 | // If the pointer type matches, just return it. |
| 752 | llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty); |
| 753 | if (PFTy == F->getType()) { |
| 754 | Entry = F; |
| 755 | } else { |
| 756 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 757 | // F is the Function* for the one with the wrong type, we must make a new |
| 758 | // Function* and update everything that used F (a declaration) with the new |
| 759 | // Function* (which will be a definition). |
| 760 | // |
| 761 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 762 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 763 | // making a new function of the correct type, RAUW, then steal the name. |
| 764 | llvm::Function *NewFn = llvm::Function::Create(FTy, |
| 765 | llvm::Function::ExternalLinkage, |
| 766 | "", &getModule()); |
| 767 | NewFn->takeName(F); |
| 768 | |
| 769 | // Replace uses of F with the Function we will endow with a body. |
| 770 | llvm::Constant *NewPtrForOldDecl = |
| 771 | llvm::ConstantExpr::getBitCast(NewFn, F->getType()); |
| 772 | F->replaceAllUsesWith(NewPtrForOldDecl); |
| 773 | |
| 774 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 775 | // really want a way to walk all of these, but we don't have it yet. This |
| 776 | // is incredibly slow! |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 777 | ReplaceMapValuesWith(F, NewFn); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 778 | |
| 779 | // Ok, delete the old function now, which is dead. |
| 780 | assert(F->isDeclaration() && "Shouldn't replace non-declaration"); |
| 781 | F->eraseFromParent(); |
| 782 | |
| 783 | SetFunctionAttributes(D, NewFn, FTy); |
| 784 | // Return the new function which has the right type. |
| 785 | Entry = NewFn; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | if (D->getAttr<AliasAttr>()) { |
| 790 | ; |
| 791 | } else { |
| 792 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
| 793 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 794 | |
| 795 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { |
| 796 | AddGlobalCtor(Fn, CA->getPriority()); |
| 797 | } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { |
| 798 | AddGlobalDtor(Fn, DA->getPriority()); |
| 799 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 803 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 804 | // Make sure that this type is translated. |
| 805 | Types.UpdateCompletedType(TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 809 | /// getBuiltinLibFunction |
| 810 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 811 | if (BuiltinID > BuiltinFunctions.size()) |
| 812 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 814 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 815 | // a slot for it. |
| 816 | assert(BuiltinID && "Invalid Builtin ID"); |
| 817 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 818 | if (FunctionSlot) |
| 819 | return FunctionSlot; |
| 820 | |
| 821 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 822 | |
| 823 | // Get the name, skip over the __builtin_ prefix. |
| 824 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 825 | |
| 826 | // Get the type for the builtin. |
| 827 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 828 | const llvm::FunctionType *Ty = |
| 829 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 830 | |
| 831 | // FIXME: This has a serious problem with code like this: |
| 832 | // void abs() {} |
| 833 | // ... __builtin_abs(x); |
| 834 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 835 | // and for the existing one to be turned into a constantexpr cast of the |
| 836 | // builtin. In the case where the existing one is a static function, it |
| 837 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 838 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 839 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 840 | return FunctionSlot = Existing; |
| 841 | assert(Existing == 0 && "FIXME: Name collision"); |
| 842 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 843 | |
| 844 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 845 | return FunctionSlot = |
| 846 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 847 | &getModule()); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 850 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 851 | unsigned NumTys) { |
| 852 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 853 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 854 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 855 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 856 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 857 | if (MemCpyFn) return MemCpyFn; |
| 858 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 859 | switch (Context.Target.getPointerWidth(0)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | default: assert(0 && "Unknown ptr width"); |
| 861 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 862 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 863 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 864 | return MemCpyFn = getIntrinsic(IID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 865 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 866 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 867 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 868 | if (MemMoveFn) return MemMoveFn; |
| 869 | llvm::Intrinsic::ID IID; |
| 870 | switch (Context.Target.getPointerWidth(0)) { |
| 871 | default: assert(0 && "Unknown ptr width"); |
| 872 | case 32: IID = llvm::Intrinsic::memmove_i32; break; |
| 873 | case 64: IID = llvm::Intrinsic::memmove_i64; break; |
| 874 | } |
| 875 | return MemMoveFn = getIntrinsic(IID); |
| 876 | } |
| 877 | |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 878 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 879 | if (MemSetFn) return MemSetFn; |
| 880 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 881 | switch (Context.Target.getPointerWidth(0)) { |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 882 | default: assert(0 && "Unknown ptr width"); |
| 883 | case 32: IID = llvm::Intrinsic::memset_i32; break; |
| 884 | case 64: IID = llvm::Intrinsic::memset_i64; break; |
| 885 | } |
| 886 | return MemSetFn = getIntrinsic(IID); |
| 887 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 888 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 889 | // FIXME: This needs moving into an Apple Objective-C runtime class |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 890 | llvm::Constant *CodeGenModule:: |
| 891 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 892 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 893 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 894 | |
| 895 | if (Entry.getValue()) |
| 896 | return Entry.getValue(); |
| 897 | |
| 898 | std::vector<llvm::Constant*> Fields; |
| 899 | |
| 900 | if (!CFConstantStringClassRef) { |
| 901 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 902 | Ty = llvm::ArrayType::get(Ty, 0); |
| 903 | |
| 904 | CFConstantStringClassRef = |
| 905 | new llvm::GlobalVariable(Ty, false, |
| 906 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 907 | "__CFConstantStringClassReference", |
| 908 | &getModule()); |
| 909 | } |
| 910 | |
| 911 | // Class pointer. |
| 912 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 913 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 914 | llvm::Constant *C = |
| 915 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 916 | Fields.push_back(C); |
| 917 | |
| 918 | // Flags. |
| 919 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 920 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 921 | |
| 922 | // String pointer. |
| 923 | C = llvm::ConstantArray::get(str); |
| 924 | C = new llvm::GlobalVariable(C->getType(), true, |
| 925 | llvm::GlobalValue::InternalLinkage, |
| 926 | C, ".str", &getModule()); |
| 927 | |
| 928 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 929 | Fields.push_back(C); |
| 930 | |
| 931 | // String length. |
| 932 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 933 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 934 | |
| 935 | // The struct. |
| 936 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 937 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 938 | llvm::GlobalVariable *GV = |
| 939 | new llvm::GlobalVariable(C->getType(), true, |
| 940 | llvm::GlobalVariable::InternalLinkage, |
| 941 | C, "", &getModule()); |
| 942 | GV->setSection("__DATA,__cfstring"); |
| 943 | Entry.setValue(GV); |
| 944 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 945 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 946 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 947 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 948 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 949 | bool constant, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 950 | CodeGenModule &CGM) { |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 951 | // Create Constant for this string literal |
| 952 | llvm::Constant *C=llvm::ConstantArray::get(str); |
| 953 | |
| 954 | // Create a global variable for this string |
| 955 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 956 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 957 | C, ".str", &CGM.getModule()); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 958 | return C; |
| 959 | } |
| 960 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 961 | /// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the character |
| 962 | /// array containing the literal. The result is pointer to array type. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 963 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 964 | // Don't share any string literals if writable-strings is turned on. |
| 965 | if (Features.WritableStrings) |
| 966 | return GenerateStringLiteral(str, false, *this); |
| 967 | |
| 968 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 969 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 970 | |
| 971 | if (Entry.getValue()) |
| 972 | return Entry.getValue(); |
| 973 | |
| 974 | // Create a global variable for this. |
| 975 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 976 | Entry.setValue(C); |
| 977 | return C; |
| 978 | } |