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 | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 17 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 18 | // Handle the Pass registration stuff necessary to use TargetData's. |
Chris Lattner | aa31ad0 | 2002-09-25 23:46:55 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | // Register the default SparcV9 implementation... |
| 21 | RegisterPass<TargetData> X("targetdata", "Target Data Layout"); |
| 22 | } |
| 23 | |
| 24 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 25 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 26 | uint64_t &Size, unsigned char &Alignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Support for StructLayout Annotation |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) |
| 33 | : Annotation(TD.getStructLayoutAID()) { |
| 34 | StructAlignment = 0; |
| 35 | StructSize = 0; |
| 36 | |
| 37 | // Loop over each of the elements, placing them in memory... |
| 38 | for (StructType::ElementTypes::const_iterator |
| 39 | TI = ST->getElementTypes().begin(), |
| 40 | TE = ST->getElementTypes().end(); TI != TE; ++TI) { |
| 41 | const Type *Ty = *TI; |
| 42 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 43 | unsigned TyAlign; |
| 44 | uint64_t TySize; |
| 45 | getTypeInfo(Ty, &TD, TySize, A); |
| 46 | TyAlign = A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 47 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 48 | // Add padding if necessary to make the data element aligned properly... |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 49 | if (StructSize % TyAlign != 0) |
| 50 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 51 | |
| 52 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 53 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 54 | |
| 55 | MemberOffsets.push_back(StructSize); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 56 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 4e840d4 | 2003-05-21 18:08:44 +0000 | [diff] [blame] | 59 | // Empty structures have alignment of 1 byte. |
| 60 | if (StructAlignment == 0) StructAlignment = 1; |
| 61 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 62 | // Add padding to the end of the struct so that it could be put in an array |
| 63 | // and all array elements would be aligned correctly. |
| 64 | if (StructSize % StructAlignment != 0) |
| 65 | StructSize = (StructSize/StructAlignment + 1) * StructAlignment; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 97b7311 | 2001-09-07 16:40:04 +0000 | [diff] [blame] | 68 | Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T, |
| 69 | void *D) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 70 | const TargetData &TD = *(const TargetData*)D; |
| 71 | assert(AID == TD.AID && "Target data annotation ID mismatch!"); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 72 | const Type *Ty = cast<Type>((const Value *)T); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 73 | assert(isa<StructType>(Ty) && |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 74 | "Can only create StructLayout annotation on structs!"); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 75 | return new StructLayout(cast<StructType>(Ty), TD); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // TargetData Class Implementation |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 82 | TargetData::TargetData(const std::string &TargetName, |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 83 | bool isLittleEndian, unsigned char PtrSize, |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 84 | unsigned char PtrAl, unsigned char DoubleAl, |
| 85 | unsigned char FloatAl, unsigned char LongAl, |
| 86 | unsigned char IntAl, unsigned char ShortAl, |
| 87 | unsigned char ByteAl) |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 88 | : AID(AnnotationManager::getID("TargetData::" + TargetName)) { |
| 89 | AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this); |
| 90 | |
Chris Lattner | 46326d9 | 2003-04-25 02:50:45 +0000 | [diff] [blame] | 91 | // If this assert triggers, a pass "required" TargetData information, but the |
| 92 | // top level tool did not provide once for it. We do not want to default |
| 93 | // construct, or else we might end up using a bad endianness or pointer size! |
| 94 | // |
| 95 | assert(!TargetName.empty() && |
| 96 | "ERROR: Tool did not specify a target data to use!"); |
| 97 | |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 98 | LittleEndian = isLittleEndian; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 99 | PointerSize = PtrSize; |
| 100 | PointerAlignment = PtrAl; |
| 101 | DoubleAlignment = DoubleAl; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 102 | assert(DoubleAlignment == PtrAl && |
| 103 | "Double alignment and pointer alignment agree for now!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 104 | FloatAlignment = FloatAl; |
| 105 | LongAlignment = LongAl; |
| 106 | IntAlignment = IntAl; |
| 107 | ShortAlignment = ShortAl; |
| 108 | ByteAlignment = ByteAl; |
| 109 | } |
| 110 | |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 111 | TargetData::TargetData(const std::string &ToolName, const Module *M) |
| 112 | : AID(AnnotationManager::getID("TargetData::" + ToolName)) { |
| 113 | AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this); |
| 114 | |
| 115 | LittleEndian = M->isLittleEndian(); |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 116 | PointerSize = M->has32BitPointers() ? 4 : 8; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 117 | PointerAlignment = PointerSize; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 118 | DoubleAlignment = PointerSize; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 119 | FloatAlignment = 4; |
| 120 | LongAlignment = 8; |
| 121 | IntAlignment = 4; |
| 122 | ShortAlignment = 2; |
| 123 | ByteAlignment = 1; |
| 124 | } |
| 125 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 126 | TargetData::~TargetData() { |
| 127 | AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory |
| 128 | } |
| 129 | |
| 130 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 131 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 132 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 133 | switch (Ty->getPrimitiveID()) { |
| 134 | case Type::VoidTyID: |
| 135 | case Type::BoolTyID: |
| 136 | case Type::UByteTyID: |
| 137 | case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return; |
| 138 | case Type::UShortTyID: |
| 139 | case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return; |
| 140 | case Type::UIntTyID: |
| 141 | case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return; |
| 142 | case Type::ULongTyID: |
| 143 | case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return; |
| 144 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return; |
| 145 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return; |
| 146 | case Type::LabelTyID: |
| 147 | case Type::PointerTyID: |
| 148 | Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment(); |
| 149 | return; |
| 150 | case Type::ArrayTyID: { |
| 151 | const ArrayType *ATy = (const ArrayType *)Ty; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 152 | getTypeInfo(ATy->getElementType(), TD, Size, Alignment); |
| 153 | Size *= ATy->getNumElements(); |
| 154 | return; |
| 155 | } |
| 156 | case Type::StructTyID: { |
| 157 | // Get the layout annotation... which is lazily created on demand. |
| 158 | const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty); |
| 159 | Size = Layout->StructSize; Alignment = Layout->StructAlignment; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | case Type::TypeTyID: |
| 164 | default: |
| 165 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 166 | return; |
| 167 | } |
| 168 | } |
| 169 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 170 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 171 | uint64_t Size; |
| 172 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 173 | getTypeInfo(Ty, this, Size, Align); |
| 174 | return Size; |
| 175 | } |
| 176 | |
| 177 | unsigned char TargetData::getTypeAlignment(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 178 | uint64_t Size; |
| 179 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 180 | getTypeInfo(Ty, this, Size, Align); |
| 181 | return Align; |
| 182 | } |
| 183 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 184 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 185 | const std::vector<Value*> &Idx) const { |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 186 | const Type *Ty = ptrTy; |
| 187 | assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 188 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 3cac88a | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 190 | for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) { |
| 191 | if (Idx[CurIDX]->getType() == Type::LongTy) { |
Vikram S. Adve | ca710e9 | 2002-08-13 18:17:56 +0000 | [diff] [blame] | 192 | // Update Ty to refer to current element |
| 193 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 194 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 195 | // Get the array index and the size of each array element. |
Chris Lattner | 3cac88a | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 196 | int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | 94a5118 | 2003-06-04 02:35:35 +0000 | [diff] [blame] | 197 | Result += arrayIdx * (int64_t)getTypeSize(Ty); |
Chris Lattner | 4954f04 | 2003-06-02 05:21:06 +0000 | [diff] [blame] | 198 | } else { |
| 199 | const StructType *STy = cast<StructType>(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 200 | assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 201 | unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 202 | |
| 203 | // Get structure layout information... |
| 204 | const StructLayout *Layout = getStructLayout(STy); |
| 205 | |
| 206 | // Add in the offset, as calculated by the structure layout info... |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 207 | assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 208 | Result += Layout->MemberOffsets[FieldNo]; |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 209 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 210 | // Update Ty to refer to current element |
| 211 | Ty = STy->getElementTypes()[FieldNo]; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | return Result; |
| 216 | } |