Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 1 | //===-- TargetData.cpp - Data size & alignment routines --------------------==// |
| 2 | // |
| 3 | // This file defines target properties related to datatype size/offset/alignment |
| 4 | // information. It uses lazy annotations to cache information about how |
| 5 | // structure types are laid out and used. |
| 6 | // |
| 7 | // This structure should be created once, filled in if the defaults are not |
| 8 | // correct and then passed around by const&. None of the members functions |
| 9 | // require modification to the object. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 13 | #include "llvm/Target/TargetData.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 14 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 16 | |
| 17 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 18 | uint64_t &Size, unsigned char &Alignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Support for StructLayout Annotation |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) |
| 25 | : Annotation(TD.getStructLayoutAID()) { |
| 26 | StructAlignment = 0; |
| 27 | StructSize = 0; |
| 28 | |
| 29 | // Loop over each of the elements, placing them in memory... |
| 30 | for (StructType::ElementTypes::const_iterator |
| 31 | TI = ST->getElementTypes().begin(), |
| 32 | TE = ST->getElementTypes().end(); TI != TE; ++TI) { |
| 33 | const Type *Ty = *TI; |
| 34 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 35 | unsigned TyAlign; |
| 36 | uint64_t TySize; |
| 37 | getTypeInfo(Ty, &TD, TySize, A); |
| 38 | TyAlign = A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 39 | |
| 40 | // Add padding if neccesary to make the data element aligned properly... |
| 41 | if (StructSize % TyAlign != 0) |
| 42 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 43 | |
| 44 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 45 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 46 | |
| 47 | MemberOffsets.push_back(StructSize); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 48 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Add padding to the end of the struct so that it could be put in an array |
| 52 | // and all array elements would be aligned correctly. |
| 53 | if (StructSize % StructAlignment != 0) |
| 54 | StructSize = (StructSize/StructAlignment + 1) * StructAlignment; |
| 55 | |
| 56 | if (StructSize == 0) { |
| 57 | StructSize = 1; // Empty struct is 1 byte |
| 58 | StructAlignment = 1; |
| 59 | } |
| 60 | } |
| 61 | |
Chris Lattner | 97b7311 | 2001-09-07 16:40:04 +0000 | [diff] [blame] | 62 | Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T, |
| 63 | void *D) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 64 | const TargetData &TD = *(const TargetData*)D; |
| 65 | assert(AID == TD.AID && "Target data annotation ID mismatch!"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 66 | const Type *Ty = cast<const Type>((const Value *)T); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 67 | assert(isa<StructType>(Ty) && |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 68 | "Can only create StructLayout annotation on structs!"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 69 | return new StructLayout((const StructType *)Ty, TD); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | // TargetData Class Implementation |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 76 | TargetData::TargetData(const std::string &TargetName, |
Chris Lattner | 0c0edf8 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 77 | unsigned char IntRegSize, unsigned char PtrSize, |
| 78 | unsigned char PtrAl, unsigned char DoubleAl, |
| 79 | unsigned char FloatAl, unsigned char LongAl, |
| 80 | unsigned char IntAl, unsigned char ShortAl, |
| 81 | unsigned char ByteAl) |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 82 | : AID(AnnotationManager::getID("TargetData::" + TargetName)) { |
| 83 | AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this); |
| 84 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 85 | IntegerRegSize = IntRegSize; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 86 | PointerSize = PtrSize; |
| 87 | PointerAlignment = PtrAl; |
| 88 | DoubleAlignment = DoubleAl; |
| 89 | FloatAlignment = FloatAl; |
| 90 | LongAlignment = LongAl; |
| 91 | IntAlignment = IntAl; |
| 92 | ShortAlignment = ShortAl; |
| 93 | ByteAlignment = ByteAl; |
| 94 | } |
| 95 | |
| 96 | TargetData::~TargetData() { |
| 97 | AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory |
| 98 | } |
| 99 | |
| 100 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 101 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 102 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 103 | switch (Ty->getPrimitiveID()) { |
| 104 | case Type::VoidTyID: |
| 105 | case Type::BoolTyID: |
| 106 | case Type::UByteTyID: |
| 107 | case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return; |
| 108 | case Type::UShortTyID: |
| 109 | case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return; |
| 110 | case Type::UIntTyID: |
| 111 | case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return; |
| 112 | case Type::ULongTyID: |
| 113 | case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return; |
| 114 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return; |
| 115 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return; |
| 116 | case Type::LabelTyID: |
| 117 | case Type::PointerTyID: |
| 118 | Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment(); |
| 119 | return; |
| 120 | case Type::ArrayTyID: { |
| 121 | const ArrayType *ATy = (const ArrayType *)Ty; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 122 | getTypeInfo(ATy->getElementType(), TD, Size, Alignment); |
| 123 | Size *= ATy->getNumElements(); |
| 124 | return; |
| 125 | } |
| 126 | case Type::StructTyID: { |
| 127 | // Get the layout annotation... which is lazily created on demand. |
| 128 | const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty); |
| 129 | Size = Layout->StructSize; Alignment = Layout->StructAlignment; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | case Type::TypeTyID: |
| 134 | default: |
| 135 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 140 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 141 | uint64_t Size; |
| 142 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 143 | getTypeInfo(Ty, this, Size, Align); |
| 144 | return Size; |
| 145 | } |
| 146 | |
| 147 | unsigned char TargetData::getTypeAlignment(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 148 | uint64_t Size; |
| 149 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 150 | getTypeInfo(Ty, this, Size, Align); |
| 151 | return Align; |
| 152 | } |
| 153 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 154 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 155 | const std::vector<Value*> &Idx) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 156 | const PointerType *PtrTy = cast<const PointerType>(ptrTy); |
| 157 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 158 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 159 | // Get the type pointed to... |
| 160 | const Type *Ty = PtrTy->getElementType(); |
| 161 | |
| 162 | for (unsigned CurIDX = 0; CurIDX < Idx.size(); ++CurIDX) { |
| 163 | if (Idx[CurIDX]->getType() == Type::UIntTy) { |
| 164 | // Get the array index and the size of each array element. |
| 165 | // Both must be known constants, or this will fail. |
| 166 | unsigned arrayIdx = cast<ConstantUInt>(Idx[CurIDX])->getValue(); |
| 167 | uint64_t elementSize = this->getTypeSize(Ty); |
| 168 | Result += arrayIdx * elementSize; |
| 169 | |
| 170 | } else if (const StructType *STy = dyn_cast<const StructType>(Ty)) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 171 | assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 172 | unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 173 | |
| 174 | // Get structure layout information... |
| 175 | const StructLayout *Layout = getStructLayout(STy); |
| 176 | |
| 177 | // Add in the offset, as calculated by the structure layout info... |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 178 | assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 179 | Result += Layout->MemberOffsets[FieldNo]; |
| 180 | |
| 181 | // Update Ty to refer to current element |
| 182 | Ty = STy->getElementTypes()[FieldNo]; |
| 183 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 184 | } else if (isa<const ArrayType>(Ty)) { |
| 185 | assert(0 && "Loading from arrays not implemented yet!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 186 | } else { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 187 | assert(0 && "Indexing type that is not struct or array?"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 188 | return 0; // Load directly through ptr |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return Result; |
| 193 | } |