CodeGenTypes.cpp revision 6b1da0ea19c12346192f5ea4d70872c13bfcc82a
1//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This is the code that handles AST -> LLVM type lowering. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CodeGenTypes.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/DeclObjC.h" 17#include "clang/AST/Expr.h" 18#include "clang/AST/RecordLayout.h" 19#include "llvm/DerivedTypes.h" 20#include "llvm/Module.h" 21#include "llvm/Target/TargetData.h" 22 23#include "CGCall.h" 24 25using namespace clang; 26using namespace CodeGen; 27 28namespace { 29 /// RecordOrganizer - This helper class, used by CGRecordLayout, layouts 30 /// structs and unions. It manages transient information used during layout. 31 /// FIXME : Handle field aligments. Handle packed structs. 32 class RecordOrganizer { 33 public: 34 explicit RecordOrganizer(CodeGenTypes &Types, const RecordDecl& Record) : 35 CGT(Types), RD(Record), STy(NULL) {} 36 37 /// layoutStructFields - Do the actual work and lay out all fields. Create 38 /// corresponding llvm struct type. This should be invoked only after 39 /// all fields are added. 40 void layoutStructFields(const ASTRecordLayout &RL); 41 42 /// layoutUnionFields - Do the actual work and lay out all fields. Create 43 /// corresponding llvm struct type. This should be invoked only after 44 /// all fields are added. 45 void layoutUnionFields(const ASTRecordLayout &RL); 46 47 /// getLLVMType - Return associated llvm struct type. This may be NULL 48 /// if fields are not laid out. 49 llvm::Type *getLLVMType() const { 50 return STy; 51 } 52 53 llvm::SmallSet<unsigned, 8> &getPaddingFields() { 54 return PaddingFields; 55 } 56 57 private: 58 CodeGenTypes &CGT; 59 const RecordDecl& RD; 60 llvm::Type *STy; 61 llvm::SmallSet<unsigned, 8> PaddingFields; 62 }; 63} 64 65CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M, 66 const llvm::TargetData &TD) 67 : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD), 68 TheABIInfo(0) { 69} 70 71CodeGenTypes::~CodeGenTypes() { 72 for(llvm::DenseMap<const Type *, CGRecordLayout *>::iterator 73 I = CGRecordLayouts.begin(), E = CGRecordLayouts.end(); 74 I != E; ++I) 75 delete I->second; 76 CGRecordLayouts.clear(); 77} 78 79/// ConvertType - Convert the specified type to its LLVM form. 80const llvm::Type *CodeGenTypes::ConvertType(QualType T) { 81 llvm::PATypeHolder Result = ConvertTypeRecursive(T); 82 83 // Any pointers that were converted defered evaluation of their pointee type, 84 // creating an opaque type instead. This is in order to avoid problems with 85 // circular types. Loop through all these defered pointees, if any, and 86 // resolve them now. 87 while (!PointersToResolve.empty()) { 88 std::pair<const PointerLikeType *, llvm::OpaqueType*> P = 89 PointersToResolve.back(); 90 PointersToResolve.pop_back(); 91 // We can handle bare pointers here because we know that the only pointers 92 // to the Opaque type are P.second and from other types. Refining the 93 // opqaue type away will invalidate P.second, but we don't mind :). 94 const llvm::Type *NT = ConvertTypeRecursive(P.first->getPointeeType()); 95 P.second->refineAbstractTypeTo(NT); 96 } 97 98 return Result; 99} 100 101const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) { 102 T = Context.getCanonicalType(T);; 103 104 // See if type is already cached. 105 llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator 106 I = TypeCache.find(T.getTypePtr()); 107 // If type is found in map and this is not a definition for a opaque 108 // place holder type then use it. Otherwise, convert type T. 109 if (I != TypeCache.end()) 110 return I->second.get(); 111 112 const llvm::Type *ResultType = ConvertNewType(T); 113 TypeCache.insert(std::make_pair(T.getTypePtr(), 114 llvm::PATypeHolder(ResultType))); 115 return ResultType; 116} 117 118/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 119/// ConvertType in that it is used to convert to the memory representation for 120/// a type. For example, the scalar representation for _Bool is i1, but the 121/// memory representation is usually i8 or i32, depending on the target. 122const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) { 123 const llvm::Type *R = ConvertType(T); 124 125 // If this is a non-bool type, don't map it. 126 if (R != llvm::Type::Int1Ty) 127 return R; 128 129 // Otherwise, return an integer of the target-specified size. 130 return llvm::IntegerType::get((unsigned)Context.getTypeSize(T)); 131 132} 133 134/// UpdateCompletedType - When we find the full definition for a TagDecl, 135/// replace the 'opaque' type we previously made for it if applicable. 136void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { 137 const Type *Key = 138 Context.getTagDeclType(const_cast<TagDecl*>(TD)).getTypePtr(); 139 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI = 140 TagDeclTypes.find(Key); 141 if (TDTI == TagDeclTypes.end()) return; 142 143 // Remember the opaque LLVM type for this tagdecl. 144 llvm::PATypeHolder OpaqueHolder = TDTI->second; 145 assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) && 146 "Updating compilation of an already non-opaque type?"); 147 148 // Remove it from TagDeclTypes so that it will be regenerated. 149 TagDeclTypes.erase(TDTI); 150 151 // Generate the new type. 152 const llvm::Type *NT = ConvertTagDeclType(TD); 153 154 // Refine the old opaque type to its new definition. 155 cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT); 156} 157 158/// Produces a vector containing the all of the instance variables in an 159/// Objective-C object, in the order that they appear. Used to create LLVM 160/// structures corresponding to Objective-C objects. 161void CodeGenTypes::CollectObjCIvarTypes(ObjCInterfaceDecl *ObjCClass, 162 std::vector<const llvm::Type*> &IvarTypes) { 163 ObjCInterfaceDecl *SuperClass = ObjCClass->getSuperClass(); 164 if (SuperClass) 165 CollectObjCIvarTypes(SuperClass, IvarTypes); 166 for (ObjCInterfaceDecl::ivar_iterator I = ObjCClass->ivar_begin(), 167 E = ObjCClass->ivar_end(); I != E; ++I) { 168 IvarTypes.push_back(ConvertType((*I)->getType())); 169 ObjCIvarInfo[*I] = IvarTypes.size() - 1; 170 } 171} 172 173static const llvm::Type* getTypeForFormat(const llvm::fltSemantics &format) { 174 if (&format == &llvm::APFloat::IEEEsingle) 175 return llvm::Type::FloatTy; 176 if (&format == &llvm::APFloat::IEEEdouble) 177 return llvm::Type::DoubleTy; 178 if (&format == &llvm::APFloat::IEEEquad) 179 return llvm::Type::FP128Ty; 180 if (&format == &llvm::APFloat::PPCDoubleDouble) 181 return llvm::Type::PPC_FP128Ty; 182 if (&format == &llvm::APFloat::x87DoubleExtended) 183 return llvm::Type::X86_FP80Ty; 184 assert(0 && "Unknown float format!"); 185 return 0; 186} 187 188const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) { 189 const clang::Type &Ty = *Context.getCanonicalType(T); 190 191 switch (Ty.getTypeClass()) { 192 case Type::TypeName: // typedef isn't canonical. 193 case Type::TypeOfExp: // typeof isn't canonical. 194 case Type::TypeOfTyp: // typeof isn't canonical. 195 assert(0 && "Non-canonical type, shouldn't happen"); 196 case Type::Builtin: { 197 switch (cast<BuiltinType>(Ty).getKind()) { 198 default: assert(0 && "Unknown builtin type!"); 199 case BuiltinType::Void: 200 // LLVM void type can only be used as the result of a function call. Just 201 // map to the same as char. 202 return llvm::IntegerType::get(8); 203 204 case BuiltinType::Bool: 205 // Note that we always return bool as i1 for use as a scalar type. 206 return llvm::Type::Int1Ty; 207 208 case BuiltinType::Char_S: 209 case BuiltinType::Char_U: 210 case BuiltinType::SChar: 211 case BuiltinType::UChar: 212 case BuiltinType::Short: 213 case BuiltinType::UShort: 214 case BuiltinType::Int: 215 case BuiltinType::UInt: 216 case BuiltinType::Long: 217 case BuiltinType::ULong: 218 case BuiltinType::LongLong: 219 case BuiltinType::ULongLong: 220 case BuiltinType::WChar: 221 return llvm::IntegerType::get( 222 static_cast<unsigned>(Context.getTypeSize(T))); 223 224 case BuiltinType::Float: 225 case BuiltinType::Double: 226 case BuiltinType::LongDouble: 227 return getTypeForFormat(Context.getFloatTypeSemantics(T)); 228 } 229 break; 230 } 231 case Type::Complex: { 232 const llvm::Type *EltTy = 233 ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType()); 234 return llvm::StructType::get(EltTy, EltTy, NULL); 235 } 236 case Type::Reference: 237 case Type::Pointer: { 238 const PointerLikeType &PTy = cast<PointerLikeType>(Ty); 239 QualType ETy = PTy.getPointeeType(); 240 llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(); 241 PointersToResolve.push_back(std::make_pair(&PTy, PointeeType)); 242 return llvm::PointerType::get(PointeeType, ETy.getAddressSpace()); 243 } 244 245 case Type::VariableArray: { 246 const VariableArrayType &A = cast<VariableArrayType>(Ty); 247 assert(A.getIndexTypeQualifier() == 0 && 248 "FIXME: We only handle trivial array types so far!"); 249 // VLAs resolve to the innermost element type; this matches 250 // the return of alloca, and there isn't any obviously better choice. 251 return ConvertTypeRecursive(A.getElementType()); 252 } 253 case Type::IncompleteArray: { 254 const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty); 255 assert(A.getIndexTypeQualifier() == 0 && 256 "FIXME: We only handle trivial array types so far!"); 257 // int X[] -> [0 x int] 258 return llvm::ArrayType::get(ConvertTypeRecursive(A.getElementType()), 0); 259 } 260 case Type::ConstantArray: { 261 const ConstantArrayType &A = cast<ConstantArrayType>(Ty); 262 const llvm::Type *EltTy = ConvertTypeRecursive(A.getElementType()); 263 return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue()); 264 } 265 case Type::ExtVector: 266 case Type::Vector: { 267 const VectorType &VT = cast<VectorType>(Ty); 268 return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()), 269 VT.getNumElements()); 270 } 271 case Type::FunctionNoProto: 272 return GetFunctionType(CGFunctionInfo(cast<FunctionTypeNoProto>(&Ty))); 273 case Type::FunctionProto: 274 return GetFunctionType(CGFunctionInfo(cast<FunctionTypeProto>(&Ty))); 275 276 case Type::ASQual: 277 return 278 ConvertTypeRecursive(QualType(cast<ASQualType>(Ty).getBaseType(), 0)); 279 280 case Type::ObjCInterface: { 281 // FIXME: This comment is broken. Either the code should check for 282 // the flag it is referring to or it should do the right thing in 283 // the presence of it. 284 285 // Warning: Use of this is strongly discouraged. Late binding of instance 286 // variables is supported on some runtimes and so using static binding can 287 // break code when libraries are updated. Only use this if you have 288 // previously checked that the ObjCRuntime subclass in use does not support 289 // late-bound ivars. 290 ObjCInterfaceType OIT = cast<ObjCInterfaceType>(Ty); 291 std::vector<const llvm::Type*> IvarTypes; 292 CollectObjCIvarTypes(OIT.getDecl(), IvarTypes); 293 llvm::Type *T = llvm::StructType::get(IvarTypes); 294 TheModule.addTypeName(std::string("struct.") + OIT.getDecl()->getName(), T); 295 return T; 296 } 297 298 case Type::ObjCQualifiedInterface: { 299 ObjCQualifiedInterfaceType QIT = cast<ObjCQualifiedInterfaceType>(Ty); 300 301 return ConvertTypeRecursive(Context.getObjCInterfaceType(QIT.getDecl())); 302 } 303 304 case Type::ObjCQualifiedId: 305 // Protocols don't influence the LLVM type. 306 return ConvertTypeRecursive(Context.getObjCIdType()); 307 308 case Type::Tagged: { 309 const TagDecl *TD = cast<TagType>(Ty).getDecl(); 310 const llvm::Type *Res = ConvertTagDeclType(TD); 311 312 std::string TypeName(TD->getKindName()); 313 TypeName += '.'; 314 315 // Name the codegen type after the typedef name 316 // if there is no tag type name available 317 if (TD->getIdentifier()) 318 TypeName += TD->getName(); 319 else if (const TypedefType *TdT = dyn_cast<TypedefType>(T)) 320 TypeName += TdT->getDecl()->getName(); 321 else 322 TypeName += "anon"; 323 324 TheModule.addTypeName(TypeName, Res); 325 return Res; 326 } 327 328 case Type::BlockPointer: { 329 assert(0 && "FIXME: Cannot get type of block pointer."); 330 } 331 } 332 333 // FIXME: implement. 334 return llvm::OpaqueType::get(); 335} 336 337/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 338/// enum. 339const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) { 340 // TagDecl's are not necessarily unique, instead use the (clang) 341 // type connected to the decl. 342 const Type *Key = 343 Context.getTagDeclType(const_cast<TagDecl*>(TD)).getTypePtr(); 344 llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI = 345 TagDeclTypes.find(Key); 346 347 // If we've already compiled this tag type, use the previous definition. 348 if (TDTI != TagDeclTypes.end()) 349 return TDTI->second; 350 351 // If this is still a forward definition, just define an opaque type to use 352 // for this tagged decl. 353 if (!TD->isDefinition()) { 354 llvm::Type *ResultType = llvm::OpaqueType::get(); 355 TagDeclTypes.insert(std::make_pair(Key, ResultType)); 356 return ResultType; 357 } 358 359 // Okay, this is a definition of a type. Compile the implementation now. 360 361 if (TD->isEnum()) { 362 // Don't bother storing enums in TagDeclTypes. 363 return ConvertTypeRecursive(cast<EnumDecl>(TD)->getIntegerType()); 364 } 365 366 // This decl could well be recursive. In this case, insert an opaque 367 // definition of this type, which the recursive uses will get. We will then 368 // refine this opaque version later. 369 370 // Create new OpaqueType now for later use in case this is a recursive 371 // type. This will later be refined to the actual type. 372 llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(); 373 TagDeclTypes.insert(std::make_pair(Key, ResultHolder)); 374 375 const llvm::Type *ResultType; 376 const RecordDecl *RD = cast<const RecordDecl>(TD); 377 if (TD->isStruct() || TD->isClass()) { 378 // Layout fields. 379 RecordOrganizer RO(*this, *RD); 380 381 RO.layoutStructFields(Context.getASTRecordLayout(RD)); 382 383 // Get llvm::StructType. 384 const Type *Key = 385 Context.getTagDeclType(const_cast<TagDecl*>(TD)).getTypePtr(); 386 CGRecordLayouts[Key] = new CGRecordLayout(RO.getLLVMType(), 387 RO.getPaddingFields()); 388 ResultType = RO.getLLVMType(); 389 390 } else if (TD->isUnion()) { 391 // Just use the largest element of the union, breaking ties with the 392 // highest aligned member. 393 if (RD->getNumMembers() != 0) { 394 RecordOrganizer RO(*this, *RD); 395 396 RO.layoutUnionFields(Context.getASTRecordLayout(RD)); 397 398 // Get llvm::StructType. 399 const Type *Key = 400 Context.getTagDeclType(const_cast<TagDecl*>(TD)).getTypePtr(); 401 CGRecordLayouts[Key] = new CGRecordLayout(RO.getLLVMType(), 402 RO.getPaddingFields()); 403 ResultType = RO.getLLVMType(); 404 } else { 405 ResultType = llvm::StructType::get(std::vector<const llvm::Type*>()); 406 } 407 } else { 408 assert(0 && "FIXME: Unknown tag decl kind!"); 409 abort(); 410 } 411 412 // Refine our Opaque type to ResultType. This can invalidate ResultType, so 413 // make sure to read the result out of the holder. 414 cast<llvm::OpaqueType>(ResultHolder.get()) 415 ->refineAbstractTypeTo(ResultType); 416 417 return ResultHolder.get(); 418} 419 420/// getLLVMFieldNo - Return llvm::StructType element number 421/// that corresponds to the field FD. 422unsigned CodeGenTypes::getLLVMFieldNo(const FieldDecl *FD) { 423 llvm::DenseMap<const FieldDecl*, unsigned>::iterator I = FieldInfo.find(FD); 424 assert (I != FieldInfo.end() && "Unable to find field info"); 425 return I->second; 426} 427 428unsigned CodeGenTypes::getLLVMFieldNo(const ObjCIvarDecl *OID) { 429 llvm::DenseMap<const ObjCIvarDecl*, unsigned>::iterator 430 I = ObjCIvarInfo.find(OID); 431 assert(I != ObjCIvarInfo.end() && "Unable to find field info"); 432 return I->second; 433} 434 435/// addFieldInfo - Assign field number to field FD. 436void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No) { 437 FieldInfo[FD] = No; 438} 439 440/// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD. 441CodeGenTypes::BitFieldInfo CodeGenTypes::getBitFieldInfo(const FieldDecl *FD) { 442 llvm::DenseMap<const FieldDecl *, BitFieldInfo>::iterator 443 I = BitFields.find(FD); 444 assert (I != BitFields.end() && "Unable to find bitfield info"); 445 return I->second; 446} 447 448/// addBitFieldInfo - Assign a start bit and a size to field FD. 449void CodeGenTypes::addBitFieldInfo(const FieldDecl *FD, unsigned Begin, 450 unsigned Size) { 451 BitFields.insert(std::make_pair(FD, BitFieldInfo(Begin, Size))); 452} 453 454/// getCGRecordLayout - Return record layout info for the given llvm::Type. 455const CGRecordLayout * 456CodeGenTypes::getCGRecordLayout(const TagDecl *TD) const { 457 const Type *Key = 458 Context.getTagDeclType(const_cast<TagDecl*>(TD)).getTypePtr(); 459 llvm::DenseMap<const Type*, CGRecordLayout *>::iterator I 460 = CGRecordLayouts.find(Key); 461 assert (I != CGRecordLayouts.end() 462 && "Unable to find record layout information for type"); 463 return I->second; 464} 465 466/// layoutStructFields - Do the actual work and lay out all fields. Create 467/// corresponding llvm struct type. 468/// Note that this doesn't actually try to do struct layout; it depends on 469/// the layout built by the AST. (We have to do struct layout to do Sema, 470/// and there's no point to duplicating the work.) 471void RecordOrganizer::layoutStructFields(const ASTRecordLayout &RL) { 472 // FIXME: This code currently always generates packed structures. 473 // Unpacked structures are more readable, and sometimes more efficient! 474 // (But note that any changes here are likely to impact CGExprConstant, 475 // which makes some messy assumptions.) 476 uint64_t llvmSize = 0; 477 // FIXME: Make this a SmallVector 478 std::vector<const llvm::Type*> LLVMFields; 479 int NumMembers = RD.getNumMembers(); 480 481 for (int curField = 0; curField < NumMembers; curField++) { 482 const FieldDecl *FD = RD.getMember(curField); 483 uint64_t offset = RL.getFieldOffset(curField); 484 const llvm::Type *Ty = CGT.ConvertTypeRecursive(FD->getType()); 485 uint64_t size = CGT.getTargetData().getABITypeSizeInBits(Ty); 486 487 if (FD->isBitField()) { 488 Expr *BitWidth = FD->getBitWidth(); 489 llvm::APSInt FieldSize(32); 490 bool isBitField = 491 BitWidth->isIntegerConstantExpr(FieldSize, CGT.getContext()); 492 assert(isBitField && "Invalid BitField size expression"); 493 isBitField=isBitField; // silence warning. 494 uint64_t BitFieldSize = FieldSize.getZExtValue(); 495 496 // Bitfield field info is different from other field info; 497 // it actually ignores the underlying LLVM struct because 498 // there isn't any convenient mapping. 499 CGT.addFieldInfo(FD, offset / size); 500 CGT.addBitFieldInfo(FD, offset % size, BitFieldSize); 501 } else { 502 // Put the element into the struct. This would be simpler 503 // if we didn't bother, but it seems a bit too strange to 504 // allocate all structs as i8 arrays. 505 while (llvmSize < offset) { 506 LLVMFields.push_back(llvm::Type::Int8Ty); 507 llvmSize += 8; 508 } 509 510 llvmSize += size; 511 CGT.addFieldInfo(FD, LLVMFields.size()); 512 LLVMFields.push_back(Ty); 513 } 514 } 515 516 while (llvmSize < RL.getSize()) { 517 LLVMFields.push_back(llvm::Type::Int8Ty); 518 llvmSize += 8; 519 } 520 521 STy = llvm::StructType::get(LLVMFields, true); 522 assert(CGT.getTargetData().getABITypeSizeInBits(STy) == RL.getSize()); 523} 524 525/// layoutUnionFields - Do the actual work and lay out all fields. Create 526/// corresponding llvm struct type. This should be invoked only after 527/// all fields are added. 528void RecordOrganizer::layoutUnionFields(const ASTRecordLayout &RL) { 529 for (int curField = 0; curField < RD.getNumMembers(); curField++) { 530 const FieldDecl *FD = RD.getMember(curField); 531 // The offset should usually be zero, but bitfields could be strange 532 uint64_t offset = RL.getFieldOffset(curField); 533 534 if (FD->isBitField()) { 535 Expr *BitWidth = FD->getBitWidth(); 536 uint64_t BitFieldSize = 537 BitWidth->getIntegerConstantExprValue(CGT.getContext()).getZExtValue(); 538 539 CGT.addFieldInfo(FD, 0); 540 CGT.addBitFieldInfo(FD, offset, BitFieldSize); 541 } else { 542 CGT.addFieldInfo(FD, 0); 543 } 544 } 545 546 // This looks stupid, but it is correct in the sense that 547 // it works no matter how complicated the sizes and alignments 548 // of the union elements are. The natural alignment 549 // of the result doesn't matter because anyone allocating 550 // structures should be aligning them appropriately anyway. 551 // FIXME: We can be a bit more intuitive in a lot of cases. 552 STy = llvm::ArrayType::get(llvm::Type::Int8Ty, RL.getSize() / 8); 553 assert(CGT.getTargetData().getABITypeSizeInBits(STy) == RL.getSize()); 554} 555