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