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