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