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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
| 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 28 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 29 | llvm::Module &M, const llvm::TargetData &TD, |
| 30 | Diagnostic &diags) |
| 31 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Devang Patel | a8fccb8 | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 32 | Types(C, M, TD), MemCpyFn(0), CFConstantStringClassRef(0) {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 34 | |
| 35 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 36 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 37 | /// them to point to NewVal. This is badbadbad, FIXME! |
| 38 | void CodeGenModule::ReplaceMapValuesWith(llvm::Constant *OldVal, |
| 39 | llvm::Constant *NewVal) { |
| 40 | for (llvm::DenseMap<const Decl*, llvm::Constant*>::iterator |
| 41 | I = GlobalDeclMap.begin(), E = GlobalDeclMap.end(); I != E; ++I) |
| 42 | if (I->second == OldVal) I->second = NewVal; |
| 43 | } |
| 44 | |
| 45 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 46 | llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D, |
| 47 | bool isDefinition) { |
| 48 | // 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] | 49 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 50 | if (Entry) return Entry; |
| 51 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 52 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 53 | |
| 54 | // Check to see if the function already exists. |
| 55 | llvm::Function *F = getModule().getFunction(D->getName()); |
| 56 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 57 | |
| 58 | // If it doesn't already exist, just create and return an entry. |
| 59 | if (F == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 60 | // FIXME: param attributes for sext/zext etc. |
| 61 | return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, |
| 62 | D->getName(), &getModule()); |
| 63 | } |
| 64 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 65 | // If the pointer type matches, just return it. |
| 66 | llvm::Type *PFTy = llvm::PointerType::get(Ty); |
| 67 | if (PFTy == F->getType()) return Entry = F; |
Chris Lattner | 77ce67c | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 69 | // If this isn't a definition, just return it casted to the right type. |
| 70 | if (!isDefinition) |
| 71 | return Entry = llvm::ConstantExpr::getBitCast(F, PFTy); |
| 72 | |
| 73 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 74 | // F is the Function* for the one with the wrong type, we must make a new |
| 75 | // Function* and update everything that used F (a declaration) with the new |
| 76 | // Function* (which will be a definition). |
| 77 | // |
| 78 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 79 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 80 | // making a new function of the correct type, RAUW, then steal the name. |
| 81 | llvm::Function *NewFn = new llvm::Function(FTy, |
| 82 | llvm::Function::ExternalLinkage, |
| 83 | "", &getModule()); |
| 84 | NewFn->takeName(F); |
| 85 | |
| 86 | // Replace uses of F with the Function we will endow with a body. |
| 87 | llvm::Constant *NewPtrForOldDecl = |
| 88 | llvm::ConstantExpr::getBitCast(NewFn, F->getType()); |
| 89 | F->replaceAllUsesWith(NewPtrForOldDecl); |
| 90 | |
| 91 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 92 | // really want a way to walk all of these, but we don't have it yet. This |
| 93 | // is incredibly slow! |
| 94 | ReplaceMapValuesWith(F, NewPtrForOldDecl); |
| 95 | |
| 96 | // Ok, delete the old function now, which is dead. |
| 97 | assert(F->isDeclaration() && "Shouldn't replace non-declaration"); |
| 98 | F->eraseFromParent(); |
| 99 | |
| 100 | // Return the new function which has the right type. |
| 101 | return Entry = NewFn; |
| 102 | } |
| 103 | |
| 104 | llvm::Constant *CodeGenModule::GetAddrOfFileVarDecl(const FileVarDecl *D, |
| 105 | bool isDefinition) { |
| 106 | // See if it is already in the map. |
| 107 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 108 | if (Entry) return Entry; |
| 109 | |
| 110 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 111 | |
| 112 | // Check to see if the global already exists. |
| 113 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(D->getName()); |
| 114 | |
| 115 | // If it doesn't already exist, just create and return an entry. |
| 116 | if (GV == 0) { |
| 117 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 118 | llvm::GlobalValue::ExternalLinkage, |
| 119 | 0, D->getName(), &getModule()); |
Chris Lattner | 77ce67c | 2007-12-02 06:30:46 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 122 | // If the pointer type matches, just return it. |
| 123 | llvm::Type *PTy = llvm::PointerType::get(Ty); |
| 124 | if (PTy == GV->getType()) return Entry = GV; |
| 125 | |
| 126 | // If this isn't a definition, just return it casted to the right type. |
| 127 | if (!isDefinition) |
| 128 | return Entry = llvm::ConstantExpr::getBitCast(GV, PTy); |
| 129 | |
| 130 | |
| 131 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 132 | // GV is the GlobalVariable* for the one with the wrong type, we must make a |
| 133 | /// new GlobalVariable* and update everything that used GV (a declaration) |
| 134 | // with the new GlobalVariable* (which will be a definition). |
| 135 | // |
| 136 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 137 | // and then a definition of a different type (e.g. "int x[10];"). Start by |
| 138 | // making a new global of the correct type, RAUW, then steal the name. |
| 139 | llvm::GlobalVariable *NewGV = |
| 140 | new llvm::GlobalVariable(Ty, false, llvm::GlobalValue::ExternalLinkage, |
| 141 | 0, D->getName(), &getModule()); |
| 142 | NewGV->takeName(GV); |
| 143 | |
| 144 | // Replace uses of GV with the globalvalue we will endow with a body. |
| 145 | llvm::Constant *NewPtrForOldDecl = |
| 146 | llvm::ConstantExpr::getBitCast(NewGV, GV->getType()); |
| 147 | GV->replaceAllUsesWith(NewPtrForOldDecl); |
| 148 | |
| 149 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 150 | // really want a way to walk all of these, but we don't have it yet. This |
| 151 | // is incredibly slow! |
| 152 | ReplaceMapValuesWith(GV, NewPtrForOldDecl); |
| 153 | |
| 154 | // Ok, delete the old global now, which is dead. |
| 155 | assert(GV->isDeclaration() && "Shouldn't replace non-declaration"); |
| 156 | GV->eraseFromParent(); |
| 157 | |
| 158 | // Return the new global which has the right type. |
| 159 | return Entry = NewGV; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 162 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
| 164 | // If this is not a prototype, emit the body. |
| 165 | if (FD->getBody()) |
| 166 | CodeGenFunction(*this).GenerateCode(FD); |
| 167 | } |
| 168 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 169 | static llvm::Constant *GenerateConstantExpr(const Expr *Expression, |
| 170 | CodeGenModule& CGModule); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 171 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 172 | /// GenerateConversionToBool - Generate comparison to zero for conversion to |
| 173 | /// bool |
| 174 | static llvm::Constant *GenerateConversionToBool(llvm::Constant *Expression, |
| 175 | QualType Source) { |
| 176 | if (Source->isRealFloatingType()) { |
| 177 | // Compare against 0.0 for fp scalars. |
| 178 | llvm::Constant *Zero = llvm::Constant::getNullValue(Expression->getType()); |
| 179 | return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Expression, |
| 180 | Zero); |
| 181 | } |
| 182 | |
| 183 | assert((Source->isIntegerType() || Source->isPointerType()) && |
| 184 | "Unknown scalar type to convert"); |
| 185 | |
| 186 | // Compare against an integer or pointer null. |
| 187 | llvm::Constant *Zero = llvm::Constant::getNullValue(Expression->getType()); |
| 188 | return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Expression, Zero); |
| 189 | } |
| 190 | |
| 191 | /// GenerateConstantCast - Generates a constant cast to convert the Expression |
| 192 | /// into the Target type. |
| 193 | static llvm::Constant *GenerateConstantCast(const Expr *Expression, |
| 194 | QualType Target, |
| 195 | CodeGenModule& CGModule) { |
| 196 | CodeGenTypes& Types = CGModule.getTypes(); |
| 197 | QualType Source = Expression->getType().getCanonicalType(); |
| 198 | Target = Target.getCanonicalType(); |
| 199 | |
| 200 | assert (!Target->isVoidType()); |
| 201 | |
| 202 | llvm::Constant *SubExpr = GenerateConstantExpr(Expression, CGModule); |
| 203 | |
| 204 | if (Source == Target) |
| 205 | return SubExpr; |
| 206 | |
| 207 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 208 | if (Target->isBooleanType()) |
| 209 | return GenerateConversionToBool(SubExpr, Source); |
| 210 | |
| 211 | const llvm::Type *SourceType = Types.ConvertType(Source); |
| 212 | const llvm::Type *TargetType = Types.ConvertType(Target); |
| 213 | |
| 214 | // Ignore conversions like int -> uint. |
| 215 | if (SubExpr->getType() == TargetType) |
| 216 | return SubExpr; |
| 217 | |
| 218 | // Handle pointer conversions next: pointers can only be converted to/from |
| 219 | // other pointers and integers. |
| 220 | if (isa<llvm::PointerType>(TargetType)) { |
| 221 | // The source value may be an integer, or a pointer. |
| 222 | if (isa<llvm::PointerType>(SubExpr->getType())) |
| 223 | return llvm::ConstantExpr::getBitCast(SubExpr, TargetType); |
| 224 | assert(Source->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 225 | return llvm::ConstantExpr::getIntToPtr(SubExpr, TargetType); |
| 226 | } |
| 227 | |
| 228 | if (isa<llvm::PointerType>(SourceType)) { |
| 229 | // Must be an ptr to int cast. |
| 230 | assert(isa<llvm::IntegerType>(TargetType) && "not ptr->int?"); |
| 231 | return llvm::ConstantExpr::getPtrToInt(SubExpr, TargetType); |
| 232 | } |
| 233 | |
| 234 | if (Source->isRealFloatingType() && Target->isRealFloatingType()) { |
| 235 | return llvm::ConstantExpr::getFPCast(SubExpr, TargetType); |
| 236 | } |
| 237 | |
| 238 | // Finally, we have the arithmetic types: real int/float. |
| 239 | if (isa<llvm::IntegerType>(SourceType)) { |
| 240 | bool InputSigned = Source->isSignedIntegerType(); |
| 241 | if (isa<llvm::IntegerType>(TargetType)) |
| 242 | return llvm::ConstantExpr::getIntegerCast(SubExpr, TargetType, |
| 243 | InputSigned); |
| 244 | else if (InputSigned) |
| 245 | return llvm::ConstantExpr::getSIToFP(SubExpr, TargetType); |
| 246 | else |
| 247 | return llvm::ConstantExpr::getUIToFP(SubExpr, TargetType); |
| 248 | } |
| 249 | |
| 250 | assert(SubExpr->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 251 | if (isa<llvm::IntegerType>(TargetType)) { |
| 252 | if (Target->isSignedIntegerType()) |
| 253 | return llvm::ConstantExpr::getFPToSI(SubExpr, TargetType); |
| 254 | else |
| 255 | return llvm::ConstantExpr::getFPToUI(SubExpr, TargetType); |
| 256 | } |
| 257 | |
| 258 | assert(TargetType->isFloatingPoint() && "Unknown real conversion"); |
| 259 | if (TargetType->getTypeID() < SubExpr->getType()->getTypeID()) |
| 260 | return llvm::ConstantExpr::getFPTrunc(SubExpr, TargetType); |
| 261 | else |
| 262 | return llvm::ConstantExpr::getFPExtend(SubExpr, TargetType); |
| 263 | |
| 264 | assert (!"Unsupported cast type in global intialiser."); |
| 265 | return 0; |
| 266 | } |
| 267 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 268 | /// GenerateAggregateInit - Generate a Constant initaliser for global array or |
| 269 | /// struct typed variables. |
| 270 | static llvm::Constant *GenerateAggregateInit(const InitListExpr *ILE, |
| 271 | CodeGenModule& CGModule) { |
| 272 | assert (ILE->getType()->isArrayType() || ILE->getType()->isStructureType()); |
| 273 | CodeGenTypes& Types = CGModule.getTypes(); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 274 | |
| 275 | unsigned NumInitElements = ILE->getNumInits(); |
| 276 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 277 | const llvm::CompositeType *CType = |
| 278 | cast<llvm::CompositeType>(Types.ConvertType(ILE->getType())); |
| 279 | assert(CType); |
| 280 | std::vector<llvm::Constant*> Elts; |
| 281 | |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 282 | // Copy initializer elements. |
| 283 | unsigned i = 0; |
| 284 | for (i = 0; i < NumInitElements; ++i) { |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 285 | llvm::Constant *C = GenerateConstantExpr(ILE->getInit(i), CGModule); |
| 286 | assert (C && "Failed to create initialiser expression"); |
| 287 | Elts.push_back(C); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 290 | if (ILE->getType()->isStructureType()) |
| 291 | return llvm::ConstantStruct::get(cast<llvm::StructType>(CType), Elts); |
| 292 | |
| 293 | // Initialising an array requires us to automatically initialise any |
| 294 | // elements that have not been initialised explicitly |
| 295 | const llvm::ArrayType *AType = cast<llvm::ArrayType>(CType); |
| 296 | assert(AType); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 297 | const llvm::Type *AElemTy = AType->getElementType(); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 298 | unsigned NumArrayElements = AType->getNumElements(); |
| 299 | // Initialize remaining array elements. |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 300 | for (; i < NumArrayElements; ++i) |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 301 | Elts.push_back(llvm::Constant::getNullValue(AElemTy)); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 302 | |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 303 | return llvm::ConstantArray::get(AType, Elts); |
| 304 | } |
| 305 | |
| 306 | /// GenerateConstantExpr - Recursively builds a constant initialiser for the |
| 307 | /// given expression. |
| 308 | static llvm::Constant *GenerateConstantExpr(const Expr* Expression, |
| 309 | CodeGenModule& CGModule) { |
| 310 | CodeGenTypes& Types = CGModule.getTypes(); |
| 311 | ASTContext& Context = CGModule.getContext(); |
| 312 | assert ((Expression->isConstantExpr(Context, 0) || |
| 313 | Expression->getStmtClass() == Stmt::InitListExprClass) && |
| 314 | "Only constant global initialisers are supported."); |
| 315 | |
| 316 | QualType type = Expression->getType().getCanonicalType(); |
| 317 | |
| 318 | if (type->isIntegerType()) { |
| 319 | llvm::APSInt |
| 320 | Value(static_cast<uint32_t>(Context.getTypeSize(type, SourceLocation()))); |
| 321 | if (Expression->isIntegerConstantExpr(Value, Context)) { |
| 322 | return llvm::ConstantInt::get(Value); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | switch (Expression->getStmtClass()) { |
| 327 | // Generate constant for floating point literal values. |
| 328 | case Stmt::FloatingLiteralClass: { |
| 329 | const FloatingLiteral *FLiteral = cast<FloatingLiteral>(Expression); |
| 330 | return llvm::ConstantFP::get(Types.ConvertType(type), FLiteral->getValue()); |
| 331 | } |
| 332 | |
| 333 | // Generate constant for string literal values. |
| 334 | case Stmt::StringLiteralClass: { |
| 335 | const StringLiteral *SLiteral = cast<StringLiteral>(Expression); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 336 | const char *StrData = SLiteral->getStrData(); |
| 337 | unsigned Len = SLiteral->getByteLength(); |
| 338 | return CGModule.GetAddrOfConstantString(std::string(StrData, |
| 339 | StrData + Len)); |
Chris Lattner | cef01ec | 2007-11-23 22:07:55 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // Elide parenthesis. |
| 343 | case Stmt::ParenExprClass: |
| 344 | return GenerateConstantExpr(cast<ParenExpr>(Expression)->getSubExpr(), |
| 345 | CGModule); |
| 346 | |
| 347 | // Generate constant for sizeof operator. |
| 348 | // FIXME: Need to support AlignOf |
| 349 | case Stmt::SizeOfAlignOfTypeExprClass: { |
| 350 | const SizeOfAlignOfTypeExpr *SOExpr = |
| 351 | cast<SizeOfAlignOfTypeExpr>(Expression); |
| 352 | assert (SOExpr->isSizeOf()); |
| 353 | return llvm::ConstantExpr::getSizeOf(Types.ConvertType(type)); |
| 354 | } |
| 355 | |
| 356 | // Generate constant cast expressions. |
| 357 | case Stmt::CastExprClass: |
| 358 | return GenerateConstantCast(cast<CastExpr>(Expression)->getSubExpr(), type, |
| 359 | CGModule); |
| 360 | |
| 361 | case Stmt::ImplicitCastExprClass: { |
| 362 | const ImplicitCastExpr *ICExpr = cast<ImplicitCastExpr>(Expression); |
| 363 | return GenerateConstantCast(ICExpr->getSubExpr(), type, CGModule); |
| 364 | } |
| 365 | |
| 366 | // Generate a constant array access expression |
| 367 | // FIXME: Clang's semantic analysis incorrectly prevents array access in |
| 368 | // global initialisers, preventing us from testing this. |
| 369 | case Stmt::ArraySubscriptExprClass: { |
| 370 | const ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(Expression); |
| 371 | llvm::Constant *Base = GenerateConstantExpr(ASExpr->getBase(), CGModule); |
| 372 | llvm::Constant *Index = GenerateConstantExpr(ASExpr->getIdx(), CGModule); |
| 373 | return llvm::ConstantExpr::getExtractElement(Base, Index); |
| 374 | } |
| 375 | |
| 376 | // Generate a constant expression to initialise an aggregate type, such as |
| 377 | // an array or struct. |
| 378 | case Stmt::InitListExprClass: |
| 379 | return GenerateAggregateInit(cast<InitListExpr>(Expression), CGModule); |
| 380 | |
| 381 | default: |
| 382 | assert (!"Unsupported expression in global initialiser."); |
| 383 | } |
| 384 | return 0; |
| 385 | } |
| 386 | |
Oliver Hunt | 253e0a7 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 387 | llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expression) { |
| 388 | return GenerateConstantExpr(Expression, *this); |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 391 | void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 392 | // If this is just a forward declaration of the variable, don't emit it now, |
| 393 | // allow it to be emitted lazily on its first use. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 394 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 395 | return; |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 396 | |
| 397 | // Get the global, forcing it to be a direct reference. |
| 398 | llvm::GlobalVariable *GV = |
| 399 | cast<llvm::GlobalVariable>(GetAddrOfFileVarDecl(D, true)); |
| 400 | |
| 401 | // Convert the initializer, or use zero if appropriate. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 402 | llvm::Constant *Init = 0; |
| 403 | if (D->getInit() == 0) { |
| 404 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
| 405 | } else if (D->getType()->isIntegerType()) { |
Hartmut Kaiser | ff08d2c | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 406 | llvm::APSInt Value(static_cast<uint32_t>( |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 407 | getContext().getTypeSize(D->getInit()->getType(), SourceLocation()))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 408 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
| 409 | Init = llvm::ConstantInt::get(Value); |
| 410 | } |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 411 | |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 412 | if (!Init) |
Oliver Hunt | 253e0a7 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 413 | Init = EmitGlobalInit(D->getInit()); |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 414 | |
Devang Patel | 08a10cc | 2007-10-30 21:27:20 +0000 | [diff] [blame] | 415 | assert(Init && "FIXME: Global variable initializers unimp!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 416 | |
| 417 | GV->setInitializer(Init); |
| 418 | |
| 419 | // Set the llvm linkage type as appropriate. |
| 420 | // FIXME: This isn't right. This should handle common linkage and other |
| 421 | // stuff. |
| 422 | switch (D->getStorageClass()) { |
| 423 | case VarDecl::Auto: |
| 424 | case VarDecl::Register: |
| 425 | assert(0 && "Can't have auto or register globals"); |
| 426 | case VarDecl::None: |
| 427 | case VarDecl::Extern: |
| 428 | // todo: common |
| 429 | break; |
| 430 | case VarDecl::Static: |
| 431 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 437 | /// declarator chain. |
| 438 | void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) { |
| 439 | for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator())) |
| 440 | EmitGlobalVar(D); |
| 441 | } |
| 442 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 443 | /// getBuiltinLibFunction |
| 444 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
| 445 | if (BuiltinFunctions.size() <= BuiltinID) |
| 446 | BuiltinFunctions.resize(BuiltinID); |
| 447 | |
| 448 | // Already available? |
| 449 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID]; |
| 450 | if (FunctionSlot) |
| 451 | return FunctionSlot; |
| 452 | |
| 453 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 454 | |
| 455 | // Get the name, skip over the __builtin_ prefix. |
| 456 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 457 | |
| 458 | // Get the type for the builtin. |
| 459 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 460 | const llvm::FunctionType *Ty = |
| 461 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 462 | |
| 463 | // FIXME: This has a serious problem with code like this: |
| 464 | // void abs() {} |
| 465 | // ... __builtin_abs(x); |
| 466 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 467 | // and for the existing one to be turned into a constantexpr cast of the |
| 468 | // builtin. In the case where the existing one is a static function, it |
| 469 | // should just be renamed. |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 470 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 471 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 472 | return FunctionSlot = Existing; |
| 473 | assert(Existing == 0 && "FIXME: Name collision"); |
| 474 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 475 | |
| 476 | // FIXME: param attributes for sext/zext etc. |
| 477 | return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage, |
| 478 | Name, &getModule()); |
| 479 | } |
| 480 | |
| 481 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 483 | if (MemCpyFn) return MemCpyFn; |
| 484 | llvm::Intrinsic::ID IID; |
| 485 | uint64_t Size; unsigned Align; |
| 486 | Context.Target.getPointerInfo(Size, Align, SourceLocation()); |
| 487 | switch (Size) { |
| 488 | default: assert(0 && "Unknown ptr width"); |
| 489 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 490 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 491 | } |
| 492 | return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID); |
| 493 | } |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 494 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 495 | llvm::Constant *CodeGenModule:: |
| 496 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 497 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 498 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 499 | |
| 500 | if (Entry.getValue()) |
| 501 | return Entry.getValue(); |
| 502 | |
| 503 | std::vector<llvm::Constant*> Fields; |
| 504 | |
| 505 | if (!CFConstantStringClassRef) { |
| 506 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 507 | Ty = llvm::ArrayType::get(Ty, 0); |
| 508 | |
| 509 | CFConstantStringClassRef = |
| 510 | new llvm::GlobalVariable(Ty, false, |
| 511 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 512 | "__CFConstantStringClassReference", |
| 513 | &getModule()); |
| 514 | } |
| 515 | |
| 516 | // Class pointer. |
| 517 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 518 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 519 | llvm::Constant *C = |
| 520 | llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2); |
| 521 | Fields.push_back(C); |
| 522 | |
| 523 | // Flags. |
| 524 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 525 | Fields.push_back(llvm::ConstantInt::get(Ty, 1992)); |
| 526 | |
| 527 | // String pointer. |
| 528 | C = llvm::ConstantArray::get(str); |
| 529 | C = new llvm::GlobalVariable(C->getType(), true, |
| 530 | llvm::GlobalValue::InternalLinkage, |
| 531 | C, ".str", &getModule()); |
| 532 | |
| 533 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 534 | Fields.push_back(C); |
| 535 | |
| 536 | // String length. |
| 537 | Ty = getTypes().ConvertType(getContext().LongTy); |
| 538 | Fields.push_back(llvm::ConstantInt::get(Ty, str.length())); |
| 539 | |
| 540 | // The struct. |
| 541 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 542 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 543 | llvm::GlobalVariable *GV = |
| 544 | new llvm::GlobalVariable(C->getType(), true, |
| 545 | llvm::GlobalVariable::InternalLinkage, |
| 546 | C, "", &getModule()); |
| 547 | GV->setSection("__DATA,__cfstring"); |
| 548 | Entry.setValue(GV); |
| 549 | return GV; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 550 | } |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 551 | |
| 552 | /// GenerateWritableString -- Creates storage for a string literal |
| 553 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 554 | bool constant, |
| 555 | CodeGenModule& CGModule) { |
| 556 | // Create Constant for this string literal |
| 557 | llvm::Constant *C=llvm::ConstantArray::get(str); |
| 558 | |
| 559 | // Create a global variable for this string |
| 560 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 561 | llvm::GlobalValue::InternalLinkage, |
| 562 | C, ".str", &CGModule.getModule()); |
| 563 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 564 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 565 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 566 | return C; |
| 567 | } |
| 568 | |
| 569 | /// CodeGenModule::GetAddrOfConstantString -- returns a pointer to the first |
| 570 | /// element of a character array containing the literal. |
| 571 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 572 | // Don't share any string literals if writable-strings is turned on. |
| 573 | if (Features.WritableStrings) |
| 574 | return GenerateStringLiteral(str, false, *this); |
| 575 | |
| 576 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 577 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 578 | |
| 579 | if (Entry.getValue()) |
| 580 | return Entry.getValue(); |
| 581 | |
| 582 | // Create a global variable for this. |
| 583 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 584 | Entry.setValue(C); |
| 585 | return C; |
| 586 | } |