blob: ed6936dd1223c75313227feb656fb5f15e055997 [file] [log] [blame]
Chris Lattnere7fb3602001-08-27 16:00:15 +00001//===-- TargetData.cpp - Data size & alignment routines --------------------==//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Lattnere7fb3602001-08-27 16:00:15 +00009//
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. Adve0799fc42001-09-18 12:58:33 +000020#include "llvm/Target/TargetData.h"
Chris Lattner53a0c382003-04-24 19:09:05 +000021#include "llvm/Module.h"
Chris Lattnere7fb3602001-08-27 16:00:15 +000022#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Chris Lattnere7fb3602001-08-27 16:00:15 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
Misha Brukman5560c9d2003-08-18 14:43:39 +000027// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000028namespace {
29 // Register the default SparcV9 implementation...
30 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
31}
32
Chris Lattnere7fb3602001-08-27 16:00:15 +000033static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +000034 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000035
36//===----------------------------------------------------------------------===//
37// Support for StructLayout Annotation
38//===----------------------------------------------------------------------===//
39
40StructLayout::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. Advef66723f2002-05-19 15:28:02 +000051 unsigned TyAlign;
52 uint64_t TySize;
53 getTypeInfo(Ty, &TD, TySize, A);
54 TyAlign = A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000055
Misha Brukman5560c9d2003-08-18 14:43:39 +000056 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000057 if (StructSize % TyAlign != 0)
58 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
59
60 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000061 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000062
63 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000064 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000065 }
66
Chris Lattner4e840d42003-05-21 18:08:44 +000067 // Empty structures have alignment of 1 byte.
68 if (StructAlignment == 0) StructAlignment = 1;
69
Chris Lattnere7fb3602001-08-27 16:00:15 +000070 // 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 Lattnere7fb3602001-08-27 16:00:15 +000074}
75
Chris Lattner97b73112001-09-07 16:40:04 +000076Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T,
77 void *D) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000078 const TargetData &TD = *(const TargetData*)D;
79 assert(AID == TD.AID && "Target data annotation ID mismatch!");
Chris Lattner949a3622003-07-23 15:30:06 +000080 const Type *Ty = cast<Type>((const Value *)T);
Chris Lattner9b625032002-05-06 16:15:30 +000081 assert(isa<StructType>(Ty) &&
Chris Lattnere7fb3602001-08-27 16:00:15 +000082 "Can only create StructLayout annotation on structs!");
Chris Lattner949a3622003-07-23 15:30:06 +000083 return new StructLayout(cast<StructType>(Ty), TD);
Chris Lattnere7fb3602001-08-27 16:00:15 +000084}
85
86//===----------------------------------------------------------------------===//
87// TargetData Class Implementation
88//===----------------------------------------------------------------------===//
89
Vikram S. Advef66723f2002-05-19 15:28:02 +000090TargetData::TargetData(const std::string &TargetName,
Chris Lattner10daaa12003-04-26 20:11:09 +000091 bool isLittleEndian, unsigned char PtrSize,
Chris Lattner85131c82002-10-14 22:41:13 +000092 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 Lattnere7fb3602001-08-27 16:00:15 +000096 : AID(AnnotationManager::getID("TargetData::" + TargetName)) {
97 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
98
Chris Lattner46326d92003-04-25 02:50:45 +000099 // 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 Lattner85131c82002-10-14 22:41:13 +0000106 LittleEndian = isLittleEndian;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000107 PointerSize = PtrSize;
108 PointerAlignment = PtrAl;
109 DoubleAlignment = DoubleAl;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000110 assert(DoubleAlignment == PtrAl &&
111 "Double alignment and pointer alignment agree for now!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000112 FloatAlignment = FloatAl;
113 LongAlignment = LongAl;
114 IntAlignment = IntAl;
115 ShortAlignment = ShortAl;
116 ByteAlignment = ByteAl;
117}
118
Chris Lattner53a0c382003-04-24 19:09:05 +0000119TargetData::TargetData(const std::string &ToolName, const Module *M)
120 : AID(AnnotationManager::getID("TargetData::" + ToolName)) {
121 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
122
Chris Lattner030574f2003-08-24 13:49:22 +0000123 LittleEndian = M->getEndianness() != Module::BigEndian;
124 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
Chris Lattner53a0c382003-04-24 19:09:05 +0000125 PointerAlignment = PointerSize;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000126 DoubleAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000127 FloatAlignment = 4;
128 LongAlignment = 8;
129 IntAlignment = 4;
130 ShortAlignment = 2;
131 ByteAlignment = 1;
132}
133
Chris Lattnere7fb3602001-08-27 16:00:15 +0000134TargetData::~TargetData() {
135 AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory
136}
137
138static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +0000139 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000140 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000141 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 Lattnere7fb3602001-08-27 16:00:15 +0000160 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. Advef66723f2002-05-19 15:28:02 +0000178uint64_t TargetData::getTypeSize(const Type *Ty) const {
179 uint64_t Size;
180 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000181 getTypeInfo(Ty, this, Size, Align);
182 return Size;
183}
184
185unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000186 uint64_t Size;
187 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000188 getTypeInfo(Ty, this, Size, Align);
189 return Align;
190}
191
Vikram S. Advef66723f2002-05-19 15:28:02 +0000192uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Chris Lattner697954c2002-01-20 22:54:45 +0000193 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000194 const Type *Ty = ptrTy;
195 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000196 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000197
Chris Lattner3cac88a2002-09-11 01:21:33 +0000198 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) {
199 if (Idx[CurIDX]->getType() == Type::LongTy) {
Vikram S. Adveca710e92002-08-13 18:17:56 +0000200 // Update Ty to refer to current element
201 Ty = cast<SequentialType>(Ty)->getElementType();
202
Vikram S. Advef66723f2002-05-19 15:28:02 +0000203 // Get the array index and the size of each array element.
Chris Lattner3cac88a2002-09-11 01:21:33 +0000204 int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
Chris Lattner94a51182003-06-04 02:35:35 +0000205 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattner4954f042003-06-02 05:21:06 +0000206 } else {
207 const StructType *STy = cast<StructType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000208 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000209 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000210
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. Advef66723f2002-05-19 15:28:02 +0000215 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000216 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000217
Chris Lattnere7fb3602001-08-27 16:00:15 +0000218 // Update Ty to refer to current element
219 Ty = STy->getElementTypes()[FieldNo];
Chris Lattnere7fb3602001-08-27 16:00:15 +0000220 }
221 }
222
223 return Result;
224}
Brian Gaeked0fde302003-11-11 22:41:34 +0000225
226} // End llvm namespace