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 | |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "CodeGenFunction.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/DerivedTypes.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" |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 30 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 31 | llvm::Module &M, const llvm::TargetData &TD, |
| 32 | Diagnostic &diags) |
| 33 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Devang Patel | a8fccb8 | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 34 | Types(C, M, TD), MemCpyFn(0), CFConstantStringClassRef(0) {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 35 | |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 36 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 37 | /// specified stmt yet. |
| 38 | void CodeGenModule::WarnUnsupported(const Stmt *S, const char *Type) { |
| 39 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 40 | "cannot codegen this %0 yet"); |
| 41 | SourceRange Range = S->getSourceRange(); |
| 42 | std::string Msg = Type; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 43 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID, |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 44 | &Msg, 1, &Range, 1); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 47 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 48 | /// specified decl yet. |
| 49 | void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) { |
| 50 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Warning, |
| 51 | "cannot codegen this %0 yet"); |
| 52 | std::string Msg = Type; |
| 53 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID, |
| 54 | &Msg, 1); |
| 55 | } |
| 56 | |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 57 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 58 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 59 | /// them to point to NewVal. This is badbadbad, FIXME! |
| 60 | void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal, |
| 61 | llvm::Constant *NewVal) { |
| 62 | for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator |
| 63 | I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I) |
| 64 | if (I->second == OldVal) I->second = NewVal; |
| 65 | } |
| 66 | |
| 67 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 68 | llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D, |
| 69 | bool isDefinition) { |
| 70 | // 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] | 71 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 72 | if (Entry) return Entry; |
| 73 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 74 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 75 | |
| 76 | // Check to see if the function already exists. |
| 77 | llvm::Function *F = getModule().getFunction(D->getName()); |
| 78 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 79 | |
| 80 | // If it doesn't already exist, just create and return an entry. |
| 81 | if (F == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 82 | // FIXME: param attributes for sext/zext etc. |
| 83 | return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, |
| 84 | D->getName(), &getModule()); |
| 85 | } |
| 86 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 87 | // If the pointer type matches, just return it. |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 88 | llvm::Type *PFTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 89 | if (PFTy == F->getType()) return Entry = F; |
Chris Lattner | 77ce67c | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 91 | // If this isn't a definition, just return it casted to the right type. |
| 92 | if (!isDefinition) |
| 93 | return Entry = llvm::ConstantExpr::getBitCast(F, PFTy); |
| 94 | |
| 95 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 96 | // F is the Function* for the one with the wrong type, we must make a new |
| 97 | // Function* and update everything that used F (a declaration) with the new |
| 98 | // Function* (which will be a definition). |
| 99 | // |
| 100 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 101 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 102 | // making a new function of the correct type, RAUW, then steal the name. |
| 103 | llvm::Function *NewFn = new llvm::Function(FTy, |
| 104 | llvm::Function::ExternalLinkage, |
| 105 | "", &getModule()); |
| 106 | NewFn->takeName(F); |
| 107 | |
| 108 | // Replace uses of F with the Function we will endow with a body. |
| 109 | llvm::Constant *NewPtrForOldDecl = |
| 110 | llvm::ConstantExpr::getBitCast(NewFn, F->getType()); |
| 111 | F->replaceAllUsesWith(NewPtrForOldDecl); |
| 112 | |
| 113 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 114 | // really want a way to walk all of these, but we don't have it yet. This |
| 115 | // is incredibly slow! |
| 116 | ReplaceMapValuesWith(F, NewPtrForOldDecl); |
| 117 | |
| 118 | // Ok, delete the old function now, which is dead. |
| 119 | assert(F->isDeclaration() && "Shouldn't replace non-declaration"); |
| 120 | F->eraseFromParent(); |
| 121 | |
| 122 | // Return the new function which has the right type. |
| 123 | return Entry = NewFn; |
| 124 | } |
| 125 | |
Chris Lattner | d2df2b5 | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 126 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 127 | bool isDefinition) { |
| 128 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 129 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 130 | // See if it is already in the map. |
| 131 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 132 | if (Entry) return Entry; |
| 133 | |
Chris Lattner | 43cdbf5 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 134 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 135 | |
| 136 | // Check to see if the global already exists. |
| 137 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName()); |
| 138 | |
| 139 | // If it doesn't already exist, just create and return an entry. |
| 140 | if (GV == 0) { |
| 141 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 142 | llvm::GlobalValue::ExternalLinkage, |
| 143 | 0, D->getName(), &getModule()); |
Chris Lattner | 77ce67c | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 146 | // If the pointer type matches, just return it. |
Christopher Lamb | 4fe5e70 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 147 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 148 | if (PTy == GV->getType()) return Entry = GV; |
| 149 | |
| 150 | // If this isn't a definition, just return it casted to the right type. |
| 151 | if (!isDefinition) |
| 152 | return Entry = llvm::ConstantExpr::getBitCast(GV, PTy); |
| 153 | |
| 154 | |
| 155 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 156 | // GV is the GlobalVariable* for the one with the wrong type, we must make a |
| 157 | /// new GlobalVariable* and update everything that used GV (a declaration) |
| 158 | // with the new GlobalVariable* (which will be a definition). |
| 159 | // |
| 160 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 161 | // and then a definition of a different type (e.g. "int x[10];"). Start by |
| 162 | // making a new global of the correct type, RAUW, then steal the name. |
| 163 | llvm::GlobalVariable *NewGV = |
| 164 | new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage, |
| 165 | 0, D->getName(), &getModule()); |
| 166 | NewGV->takeName(GV); |
| 167 | |
| 168 | // Replace uses of GV with the globalvalue we will endow with a body. |
| 169 | llvm::Constant *NewPtrForOldDecl = |
| 170 | llvm::ConstantExpr::getBitCast(NewGV, GV->getType()); |
| 171 | GV->replaceAllUsesWith(NewPtrForOldDecl); |
| 172 | |
| 173 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 174 | // really want a way to walk all of these, but we don't have it yet. This |
| 175 | // is incredibly slow! |
| 176 | ReplaceMapValuesWith(GV, NewPtrForOldDecl); |
| 177 | |
| 178 | // Ok, delete the old global now, which is dead. |
| 179 | assert(GV->isDeclaration() && "Shouldn't replace non-declaration"); |
| 180 | GV->eraseFromParent(); |
| 181 | |
| 182 | // Return the new global which has the right type. |
| 183 | return Entry = NewGV; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 187 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
| 188 | // If this is not a prototype, emit the body. |
| 189 | if (FD->getBody()) |
| 190 | CodeGenFunction(*this).GenerateCode(FD); |
| 191 | } |
| 192 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 193 | static llvm::Constant *GenerateConstantExpr(const Expr *Expression, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 194 | CodeGenModule &CGM); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 195 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 196 | /// GenerateConversionToBool - Generate comparison to zero for conversion to |
| 197 | /// bool |
| 198 | static llvm::Constant *GenerateConversionToBool(llvm::Constant *Expression, |
| 199 | QualType Source) { |
| 200 | if (Source->isRealFloatingType()) { |
| 201 | // Compare against 0.0 for fp scalars. |
| 202 | llvm::Constant *Zero = llvm::Constant::getNullValue(Expression->getType()); |
| 203 | return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Expression, |
| 204 | Zero); |
| 205 | } |
| 206 | |
| 207 | assert((Source->isIntegerType() || Source->isPointerType()) && |
| 208 | "Unknown scalar type to convert"); |
| 209 | |
| 210 | // Compare against an integer or pointer null. |
| 211 | llvm::Constant *Zero = llvm::Constant::getNullValue(Expression->getType()); |
| 212 | return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Expression, Zero); |
| 213 | } |
| 214 | |
| 215 | /// GenerateConstantCast - Generates a constant cast to convert the Expression |
| 216 | /// into the Target type. |
| 217 | static llvm::Constant *GenerateConstantCast(const Expr *Expression, |
Chris Lattner | 2ab28c9 | 2007-12-09 00:36:01 +0000 | [diff] [blame] | 218 | QualType Target, |
| 219 | CodeGenModule &CGM) { |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 220 | CodeGenTypes& Types = CGM.getTypes(); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 221 | QualType Source = Expression->getType().getCanonicalType(); |
| 222 | Target = Target.getCanonicalType(); |
| 223 | |
| 224 | assert (!Target->isVoidType()); |
| 225 | |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 226 | llvm::Constant *SubExpr = GenerateConstantExpr(Expression, CGM); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 227 | |
| 228 | if (Source == Target) |
| 229 | return SubExpr; |
| 230 | |
| 231 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 232 | if (Target->isBooleanType()) |
| 233 | return GenerateConversionToBool(SubExpr, Source); |
| 234 | |
| 235 | const llvm::Type *SourceType = Types.ConvertType(Source); |
| 236 | const llvm::Type *TargetType = Types.ConvertType(Target); |
| 237 | |
| 238 | // Ignore conversions like int -> uint. |
| 239 | if (SubExpr->getType() == TargetType) |
| 240 | return SubExpr; |
| 241 | |
| 242 | // Handle pointer conversions next: pointers can only be converted to/from |
| 243 | // other pointers and integers. |
| 244 | if (isa<llvm::PointerType>(TargetType)) { |
| 245 | // The source value may be an integer, or a pointer. |
| 246 | if (isa<llvm::PointerType>(SubExpr->getType())) |
| 247 | return llvm::ConstantExpr::getBitCast(SubExpr, TargetType); |
| 248 | assert(Source->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 249 | return llvm::ConstantExpr::getIntToPtr(SubExpr, TargetType); |
| 250 | } |
| 251 | |
| 252 | if (isa<llvm::PointerType>(SourceType)) { |
| 253 | // Must be an ptr to int cast. |
| 254 | assert(isa<llvm::IntegerType>(TargetType) && "not ptr->int?"); |
| 255 | return llvm::ConstantExpr::getPtrToInt(SubExpr, TargetType); |
| 256 | } |
| 257 | |
| 258 | if (Source->isRealFloatingType() && Target->isRealFloatingType()) { |
| 259 | return llvm::ConstantExpr::getFPCast(SubExpr, TargetType); |
| 260 | } |
| 261 | |
| 262 | // Finally, we have the arithmetic types: real int/float. |
| 263 | if (isa<llvm::IntegerType>(SourceType)) { |
| 264 | bool InputSigned = Source->isSignedIntegerType(); |
| 265 | if (isa<llvm::IntegerType>(TargetType)) |
| 266 | return llvm::ConstantExpr::getIntegerCast(SubExpr, TargetType, |
| 267 | InputSigned); |
| 268 | else if (InputSigned) |
| 269 | return llvm::ConstantExpr::getSIToFP(SubExpr, TargetType); |
| 270 | else |
| 271 | return llvm::ConstantExpr::getUIToFP(SubExpr, TargetType); |
| 272 | } |
| 273 | |
| 274 | assert(SubExpr->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 275 | if (isa<llvm::IntegerType>(TargetType)) { |
| 276 | if (Target->isSignedIntegerType()) |
| 277 | return llvm::ConstantExpr::getFPToSI(SubExpr, TargetType); |
| 278 | else |
| 279 | return llvm::ConstantExpr::getFPToUI(SubExpr, TargetType); |
| 280 | } |
| 281 | |
| 282 | assert(TargetType->isFloatingPoint() && "Unknown real conversion"); |
| 283 | if (TargetType->getTypeID() < SubExpr->getType()->getTypeID()) |
| 284 | return llvm::ConstantExpr::getFPTrunc(SubExpr, TargetType); |
| 285 | else |
| 286 | return llvm::ConstantExpr::getFPExtend(SubExpr, TargetType); |
| 287 | |
| 288 | assert (!"Unsupported cast type in global intialiser."); |
| 289 | return 0; |
| 290 | } |
| 291 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 292 | /// GenerateAggregateInit - Generate a Constant initaliser for global array or |
| 293 | /// struct typed variables. |
| 294 | static llvm::Constant *GenerateAggregateInit(const InitListExpr *ILE, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 295 | CodeGenModule &CGM) { |
Chris Lattner | f93e6db | 2007-12-17 05:17:42 +0000 | [diff] [blame] | 296 | if (ILE->getType()->isVoidType()) { |
| 297 | // FIXME: Remove this when sema of initializers is finished (and the code |
| 298 | // below). |
| 299 | CGM.WarnUnsupported(ILE, "initializer"); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | assert((ILE->getType()->isArrayType() || ILE->getType()->isStructureType()) && |
| 304 | "Bad type for init list!"); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 305 | CodeGenTypes& Types = CGM.getTypes(); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 306 | |
| 307 | unsigned NumInitElements = ILE->getNumInits(); |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 308 | unsigned NumInitableElts = NumInitElements; |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 309 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 310 | const llvm::CompositeType *CType = |
| 311 | cast<llvm::CompositeType>(Types.ConvertType(ILE->getType())); |
| 312 | assert(CType); |
| 313 | std::vector<llvm::Constant*> Elts; |
| 314 | |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 315 | // Initialising an array requires us to automatically initialise any |
| 316 | // elements that have not been initialised explicitly |
| 317 | const llvm::ArrayType *AType = 0; |
| 318 | const llvm::Type *AElemTy = 0; |
| 319 | unsigned NumArrayElements = 0; |
| 320 | |
| 321 | // If this is an array, we may have to truncate the initializer |
| 322 | if ((AType = dyn_cast<llvm::ArrayType>(CType))) { |
| 323 | NumArrayElements = AType->getNumElements(); |
| 324 | AElemTy = AType->getElementType(); |
| 325 | NumInitableElts = std::min(NumInitableElts, NumArrayElements); |
| 326 | } |
| 327 | |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 328 | // Copy initializer elements. |
| 329 | unsigned i = 0; |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 330 | for (i = 0; i < NumInitableElts; ++i) { |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 331 | llvm::Constant *C = GenerateConstantExpr(ILE->getInit(i), CGM); |
Chris Lattner | f93e6db | 2007-12-17 05:17:42 +0000 | [diff] [blame] | 332 | // FIXME: Remove this when sema of initializers is finished (and the code |
| 333 | // above). |
| 334 | if (C == 0 && ILE->getInit(i)->getType()->isVoidType()) { |
| 335 | if (ILE->getType()->isVoidType()) return 0; |
| 336 | return llvm::UndefValue::get(CType); |
| 337 | } |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 338 | assert (C && "Failed to create initialiser expression"); |
| 339 | Elts.push_back(C); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 342 | if (ILE->getType()->isStructureType()) |
| 343 | return llvm::ConstantStruct::get(cast<llvm::StructType>(CType), Elts); |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 344 | |
| 345 | // Make sure we have an array at this point |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 346 | assert(AType); |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 347 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 348 | // Initialize remaining array elements. |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 349 | for (; i < NumArrayElements; ++i) |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 350 | Elts.push_back(llvm::Constant::getNullValue(AElemTy)); |
Christopher Lamb | 6db92f3 | 2007-12-02 08:49:54 +0000 | [diff] [blame] | 351 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 352 | return llvm::ConstantArray::get(AType, Elts); |
| 353 | } |
| 354 | |
| 355 | /// GenerateConstantExpr - Recursively builds a constant initialiser for the |
| 356 | /// given expression. |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 357 | static llvm::Constant *GenerateConstantExpr(const Expr *Expression, |
| 358 | CodeGenModule &CGM) { |
| 359 | CodeGenTypes& Types = CGM.getTypes(); |
| 360 | ASTContext& Context = CGM.getContext(); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 361 | assert ((Expression->isConstantExpr(Context, 0) || |
| 362 | Expression->getStmtClass() == Stmt::InitListExprClass) && |
| 363 | "Only constant global initialisers are supported."); |
| 364 | |
| 365 | QualType type = Expression->getType().getCanonicalType(); |
| 366 | |
| 367 | if (type->isIntegerType()) { |
| 368 | llvm::APSInt |
| 369 | Value(static_cast<uint32_t>(Context.getTypeSize(type, SourceLocation()))); |
| 370 | if (Expression->isIntegerConstantExpr(Value, Context)) { |
| 371 | return llvm::ConstantInt::get(Value); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | switch (Expression->getStmtClass()) { |
Chris Lattner | 2ab28c9 | 2007-12-09 00:36:01 +0000 | [diff] [blame] | 376 | default: break; // default emits a warning and returns bogus value. |
| 377 | case Stmt::DeclRefExprClass: { |
| 378 | const ValueDecl *Decl = cast<DeclRefExpr>(Expression)->getDecl(); |
| 379 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
| 380 | return CGM.GetAddrOfFunctionDecl(FD, false); |
| 381 | break; |
| 382 | } |
| 383 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 384 | // Generate constant for floating point literal values. |
| 385 | case Stmt::FloatingLiteralClass: { |
| 386 | const FloatingLiteral *FLiteral = cast<FloatingLiteral>(Expression); |
| 387 | return llvm::ConstantFP::get(Types.ConvertType(type), FLiteral->getValue()); |
| 388 | } |
| 389 | |
| 390 | // Generate constant for string literal values. |
| 391 | case Stmt::StringLiteralClass: { |
Chris Lattner | ff1ccb0 | 2007-12-11 01:38:45 +0000 | [diff] [blame] | 392 | const StringLiteral *String = cast<StringLiteral>(Expression); |
| 393 | const char *StrData = String->getStrData(); |
| 394 | unsigned Len = String->getByteLength(); |
| 395 | |
| 396 | // If the string has a pointer type, emit it as a global and use the pointer |
| 397 | // to the global as its value. |
| 398 | if (String->getType()->isPointerType()) |
| 399 | return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len)); |
| 400 | |
| 401 | // Otherwise this must be a string initializing an array in a static |
| 402 | // initializer. Don't emit it as the address of the string, emit the string |
| 403 | // data itself as an inline array. |
| 404 | const ConstantArrayType *CAT = String->getType()->getAsConstantArrayType(); |
| 405 | assert(CAT && "String isn't pointer or array!"); |
| 406 | |
| 407 | std::string Str(StrData, StrData + Len); |
| 408 | // Null terminate the string before potentially truncating it. |
| 409 | // FIXME: What about wchar_t strings? |
| 410 | Str.push_back(0); |
| 411 | |
| 412 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 413 | // String or grow the initializer to the required size. |
| 414 | if (RealLen != Str.size()) |
| 415 | Str.resize(RealLen); |
| 416 | |
| 417 | return llvm::ConstantArray::get(Str, false); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Elide parenthesis. |
| 421 | case Stmt::ParenExprClass: |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 422 | return GenerateConstantExpr(cast<ParenExpr>(Expression)->getSubExpr(), CGM); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 423 | |
| 424 | // Generate constant for sizeof operator. |
| 425 | // FIXME: Need to support AlignOf |
| 426 | case Stmt::SizeOfAlignOfTypeExprClass: { |
| 427 | const SizeOfAlignOfTypeExpr *SOExpr = |
| 428 | cast<SizeOfAlignOfTypeExpr>(Expression); |
| 429 | assert (SOExpr->isSizeOf()); |
| 430 | return llvm::ConstantExpr::getSizeOf(Types.ConvertType(type)); |
| 431 | } |
| 432 | |
| 433 | // Generate constant cast expressions. |
| 434 | case Stmt::CastExprClass: |
| 435 | return GenerateConstantCast(cast<CastExpr>(Expression)->getSubExpr(), type, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 436 | CGM); |
Chris Lattner | 62891ad | 2007-12-29 23:43:37 +0000 | [diff] [blame] | 437 | case Stmt::UnaryOperatorClass: { |
| 438 | const UnaryOperator *Op = cast<UnaryOperator>(Expression); |
| 439 | llvm::Constant *SubExpr = GenerateConstantExpr(Op->getSubExpr(), CGM); |
| 440 | // FIXME: These aren't right for complex. |
| 441 | switch (Op->getOpcode()) { |
| 442 | default: break; |
| 443 | case UnaryOperator::Plus: |
| 444 | case UnaryOperator::Extension: |
| 445 | return SubExpr; |
| 446 | case UnaryOperator::Minus: |
| 447 | return llvm::ConstantExpr::getNeg(SubExpr); |
| 448 | case UnaryOperator::Not: |
| 449 | return llvm::ConstantExpr::getNot(SubExpr); |
| 450 | case UnaryOperator::LNot: |
| 451 | if (Op->getSubExpr()->getType()->isRealFloatingType()) { |
| 452 | // Compare against 0.0 for fp scalars. |
| 453 | llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType()); |
| 454 | SubExpr = llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, SubExpr, |
| 455 | Zero); |
| 456 | } else { |
| 457 | assert((Op->getSubExpr()->getType()->isIntegerType() || |
| 458 | Op->getSubExpr()->getType()->isPointerType()) && |
| 459 | "Unknown scalar type to convert"); |
| 460 | // Compare against an integer or pointer null. |
| 461 | llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType()); |
| 462 | SubExpr = llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, SubExpr, |
| 463 | Zero); |
| 464 | } |
| 465 | |
| 466 | return llvm::ConstantExpr::getZExt(SubExpr, Types.ConvertType(type)); |
| 467 | //SizeOf, AlignOf, // [C99 6.5.3.4] Sizeof (expr, not type) operator. |
| 468 | //Real, Imag, // "__real expr"/"__imag expr" Extension. |
| 469 | //OffsetOf // __builtin_offsetof |
| 470 | } |
| 471 | break; |
| 472 | } |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 473 | case Stmt::ImplicitCastExprClass: { |
| 474 | const ImplicitCastExpr *ICExpr = cast<ImplicitCastExpr>(Expression); |
Chris Lattner | b656f7d | 2007-12-02 07:30:13 +0000 | [diff] [blame] | 475 | |
| 476 | // If this is due to array->pointer conversion, emit the array expression as |
| 477 | // an l-value. |
| 478 | if (ICExpr->getSubExpr()->getType()->isArrayType()) { |
Chris Lattner | 21811c7 | 2007-12-02 07:32:25 +0000 | [diff] [blame] | 479 | // Note that VLAs can't exist for global variables. |
Chris Lattner | b656f7d | 2007-12-02 07:30:13 +0000 | [diff] [blame] | 480 | // The only thing that can have array type like this is a |
| 481 | // DeclRefExpr(FileVarDecl)? |
| 482 | const DeclRefExpr *DRE = cast<DeclRefExpr>(ICExpr->getSubExpr()); |
Chris Lattner | d2df2b5 | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 483 | const VarDecl *VD = cast<VarDecl>(DRE->getDecl()); |
| 484 | llvm::Constant *C = CGM.GetAddrOfGlobalVar(VD, false); |
Chris Lattner | b656f7d | 2007-12-02 07:30:13 +0000 | [diff] [blame] | 485 | assert(isa<llvm::PointerType>(C->getType()) && |
| 486 | isa<llvm::ArrayType>(cast<llvm::PointerType>(C->getType()) |
Chris Lattner | 21811c7 | 2007-12-02 07:32:25 +0000 | [diff] [blame] | 487 | ->getElementType())); |
Chris Lattner | b656f7d | 2007-12-02 07:30:13 +0000 | [diff] [blame] | 488 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 489 | |
| 490 | llvm::Constant *Ops[] = {Idx0, Idx0}; |
Chris Lattner | f67265f | 2007-12-10 19:50:32 +0000 | [diff] [blame] | 491 | C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
| 492 | |
| 493 | // The resultant pointer type can be implicitly casted to other pointer |
| 494 | // types as well, for example void*. |
| 495 | const llvm::Type *DestPTy = Types.ConvertType(type); |
| 496 | assert(isa<llvm::PointerType>(DestPTy) && |
| 497 | "Only expect implicit cast to pointer"); |
| 498 | return llvm::ConstantExpr::getBitCast(C, DestPTy); |
Chris Lattner | b656f7d | 2007-12-02 07:30:13 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 501 | return GenerateConstantCast(ICExpr->getSubExpr(), type, CGM); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Generate a constant array access expression |
| 505 | // FIXME: Clang's semantic analysis incorrectly prevents array access in |
| 506 | // global initialisers, preventing us from testing this. |
| 507 | case Stmt::ArraySubscriptExprClass: { |
| 508 | const ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(Expression); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 509 | llvm::Constant *Base = GenerateConstantExpr(ASExpr->getBase(), CGM); |
| 510 | llvm::Constant *Index = GenerateConstantExpr(ASExpr->getIdx(), CGM); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 511 | return llvm::ConstantExpr::getExtractElement(Base, Index); |
| 512 | } |
| 513 | |
| 514 | // Generate a constant expression to initialise an aggregate type, such as |
| 515 | // an array or struct. |
| 516 | case Stmt::InitListExprClass: |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 517 | return GenerateAggregateInit(cast<InitListExpr>(Expression), CGM); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 518 | } |
Chris Lattner | 2ab28c9 | 2007-12-09 00:36:01 +0000 | [diff] [blame] | 519 | |
| 520 | CGM.WarnUnsupported(Expression, "initializer"); |
| 521 | return llvm::UndefValue::get(Types.ConvertType(type)); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Oliver Hunt | 253e0a7 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 524 | llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expression) { |
| 525 | return GenerateConstantExpr(Expression, *this); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 528 | void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 529 | // If this is just a forward declaration of the variable, don't emit it now, |
| 530 | // allow it to be emitted lazily on its first use. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 531 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 532 | return; |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 533 | |
| 534 | // Get the global, forcing it to be a direct reference. |
| 535 | llvm::GlobalVariable *GV = |
Chris Lattner | d2df2b5 | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 536 | cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true)); |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 537 | |
| 538 | // Convert the initializer, or use zero if appropriate. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 539 | llvm::Constant *Init = 0; |
| 540 | if (D->getInit() == 0) { |
| 541 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
| 542 | } else if (D->getType()->isIntegerType()) { |
Hartmut Kaiser | ff08d2c | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 543 | llvm::APSInt Value(static_cast<uint32_t>( |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 544 | getContext().getTypeSize(D->getInit()->getType(), SourceLocation()))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 545 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
| 546 | Init = llvm::ConstantInt::get(Value); |
| 547 | } |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 548 | |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 549 | if (!Init) |
Oliver Hunt | 253e0a7 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 550 | Init = EmitGlobalInit(D->getInit()); |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 551 | |
Chris Lattner | c7e4f67 | 2007-12-10 00:05:55 +0000 | [diff] [blame] | 552 | assert(GV->getType()->getElementType() == Init->getType() && |
| 553 | "Initializer codegen type mismatch!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 554 | GV->setInitializer(Init); |
| 555 | |
| 556 | // Set the llvm linkage type as appropriate. |
| 557 | // FIXME: This isn't right. This should handle common linkage and other |
| 558 | // stuff. |
| 559 | switch (D->getStorageClass()) { |
| 560 | case VarDecl::Auto: |
| 561 | case VarDecl::Register: |
| 562 | assert(0 && "Can't have auto or register globals"); |
| 563 | case VarDecl::None: |
| 564 | case VarDecl::Extern: |
| 565 | // todo: common |
| 566 | break; |
| 567 | case VarDecl::Static: |
| 568 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 574 | /// declarator chain. |
| 575 | void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) { |
| 576 | for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator())) |
| 577 | EmitGlobalVar(D); |
| 578 | } |
| 579 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 580 | /// getBuiltinLibFunction |
| 581 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 582 | if (BuiltinID > BuiltinFunctions.size()) |
| 583 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 585 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 586 | // a slot for it. |
| 587 | assert(BuiltinID && "Invalid Builtin ID"); |
| 588 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 589 | if (FunctionSlot) |
| 590 | return FunctionSlot; |
| 591 | |
| 592 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 593 | |
| 594 | // Get the name, skip over the __builtin_ prefix. |
| 595 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 596 | |
| 597 | // Get the type for the builtin. |
| 598 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 599 | const llvm::FunctionType *Ty = |
| 600 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 601 | |
| 602 | // FIXME: This has a serious problem with code like this: |
| 603 | // void abs() {} |
| 604 | // ... __builtin_abs(x); |
| 605 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 606 | // and for the existing one to be turned into a constantexpr cast of the |
| 607 | // builtin. In the case where the existing one is a static function, it |
| 608 | // should just be renamed. |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 609 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 610 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 611 | return FunctionSlot = Existing; |
| 612 | assert(Existing == 0 && "FIXME: Name collision"); |
| 613 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 614 | |
| 615 | // FIXME: param attributes for sext/zext etc. |
| 616 | return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage, |
| 617 | Name, &getModule()); |
| 618 | } |
| 619 | |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 620 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 621 | unsigned NumTys) { |
| 622 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 623 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 624 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 626 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 627 | if (MemCpyFn) return MemCpyFn; |
| 628 | llvm::Intrinsic::ID IID; |
| 629 | uint64_t Size; unsigned Align; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 630 | Context.Target.getPointerInfo(Size, Align, FullSourceLoc()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 631 | switch (Size) { |
| 632 | default: assert(0 && "Unknown ptr width"); |
| 633 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 634 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 635 | } |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 636 | return MemCpyFn = getIntrinsic(IID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 637 | } |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 639 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 640 | llvm::Constant *CodeGenModule:: |
| 641 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 642 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 643 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 644 | |
| 645 | if (Entry.getValue()) |
| 646 | return Entry.getValue(); |
| 647 | |
| 648 | std::vector<llvm::Constant*> Fields; |
| 649 | |
| 650 | if (!CFConstantStringClassRef) { |
| 651 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 652 | Ty = llvm::ArrayType::get(Ty, 0); |
| 653 | |
| 654 | CFConstantStringClassRef = |
| 655 | new llvm::GlobalVariable(Ty, false, |
| 656 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 657 | "__CFConstantStringClassReference", |
| 658 | &getModule()); |
| 659 | } |
| 660 | |
| 661 | // Class pointer. |
| 662 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 663 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 664 | llvm::Constant *C = |
| 665 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 666 | Fields.push_back(C); |
| 667 | |
| 668 | // Flags. |
| 669 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 670 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 671 | |
| 672 | // String pointer. |
| 673 | C = llvm::ConstantArray::get(str); |
| 674 | C = new llvm::GlobalVariable(C->getType(), true, |
| 675 | llvm::GlobalValue::InternalLinkage, |
| 676 | C, ".str", &getModule()); |
| 677 | |
| 678 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 679 | Fields.push_back(C); |
| 680 | |
| 681 | // String length. |
| 682 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 683 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 684 | |
| 685 | // The struct. |
| 686 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 687 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 688 | llvm::GlobalVariable *GV = |
| 689 | new llvm::GlobalVariable(C->getType(), true, |
| 690 | llvm::GlobalVariable::InternalLinkage, |
| 691 | C, "", &getModule()); |
| 692 | GV->setSection("__DATA,__cfstring"); |
| 693 | Entry.setValue(GV); |
| 694 | return GV; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 696 | |
| 697 | /// GenerateWritableString -- Creates storage for a string literal |
| 698 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 699 | bool constant, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 700 | CodeGenModule &CGM) { |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 701 | // Create Constant for this string literal |
| 702 | llvm::Constant *C=llvm::ConstantArray::get(str); |
| 703 | |
| 704 | // Create a global variable for this string |
| 705 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 706 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 707 | C, ".str", &CGM.getModule()); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 708 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 709 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 710 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 711 | return C; |
| 712 | } |
| 713 | |
| 714 | /// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the first |
| 715 | /// element of a character array containing the literal. |
| 716 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 717 | // Don't share any string literals if writable-strings is turned on. |
| 718 | if (Features.WritableStrings) |
| 719 | return GenerateStringLiteral(str, false, *this); |
| 720 | |
| 721 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 722 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 723 | |
| 724 | if (Entry.getValue()) |
| 725 | return Entry.getValue(); |
| 726 | |
| 727 | // Create a global variable for this. |
| 728 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 729 | Entry.setValue(C); |
| 730 | return C; |
| 731 | } |