blob: 9142f1dfc1cd704adf8d27b60d3d34154aaa7406 [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 Lattnerf0453282003-12-22 05:01:15 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Misha Brukman5560c9d2003-08-18 14:43:39 +000026// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000027namespace {
28 // Register the default SparcV9 implementation...
29 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
30}
31
Chris Lattnere7fb3602001-08-27 16:00:15 +000032static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +000033 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000034
35//===----------------------------------------------------------------------===//
36// Support for StructLayout Annotation
37//===----------------------------------------------------------------------===//
38
39StructLayout::StructLayout(const StructType *ST, const TargetData &TD)
40 : Annotation(TD.getStructLayoutAID()) {
41 StructAlignment = 0;
42 StructSize = 0;
43
44 // Loop over each of the elements, placing them in memory...
Chris Lattnerd21cd802004-02-09 04:37:31 +000045 for (StructType::element_iterator TI = ST->element_begin(),
46 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000047 const Type *Ty = *TI;
48 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000049 unsigned TyAlign;
50 uint64_t TySize;
51 getTypeInfo(Ty, &TD, TySize, A);
52 TyAlign = A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000053
Misha Brukman5560c9d2003-08-18 14:43:39 +000054 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000055 if (StructSize % TyAlign != 0)
56 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
57
58 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000059 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000060
61 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000062 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000063 }
64
Chris Lattner4e840d42003-05-21 18:08:44 +000065 // Empty structures have alignment of 1 byte.
66 if (StructAlignment == 0) StructAlignment = 1;
67
Chris Lattnere7fb3602001-08-27 16:00:15 +000068 // Add padding to the end of the struct so that it could be put in an array
69 // and all array elements would be aligned correctly.
70 if (StructSize % StructAlignment != 0)
71 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000072}
73
Chris Lattner97b73112001-09-07 16:40:04 +000074Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T,
75 void *D) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000076 const TargetData &TD = *(const TargetData*)D;
77 assert(AID == TD.AID && "Target data annotation ID mismatch!");
Chris Lattner949a3622003-07-23 15:30:06 +000078 const Type *Ty = cast<Type>((const Value *)T);
Chris Lattner9b625032002-05-06 16:15:30 +000079 assert(isa<StructType>(Ty) &&
Chris Lattnere7fb3602001-08-27 16:00:15 +000080 "Can only create StructLayout annotation on structs!");
Chris Lattner949a3622003-07-23 15:30:06 +000081 return new StructLayout(cast<StructType>(Ty), TD);
Chris Lattnere7fb3602001-08-27 16:00:15 +000082}
83
84//===----------------------------------------------------------------------===//
85// TargetData Class Implementation
86//===----------------------------------------------------------------------===//
87
Vikram S. Advef66723f2002-05-19 15:28:02 +000088TargetData::TargetData(const std::string &TargetName,
Chris Lattner10daaa12003-04-26 20:11:09 +000089 bool isLittleEndian, unsigned char PtrSize,
Chris Lattner85131c82002-10-14 22:41:13 +000090 unsigned char PtrAl, unsigned char DoubleAl,
91 unsigned char FloatAl, unsigned char LongAl,
92 unsigned char IntAl, unsigned char ShortAl,
93 unsigned char ByteAl)
Chris Lattnere7fb3602001-08-27 16:00:15 +000094 : AID(AnnotationManager::getID("TargetData::" + TargetName)) {
95 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
96
Chris Lattner46326d92003-04-25 02:50:45 +000097 // If this assert triggers, a pass "required" TargetData information, but the
98 // top level tool did not provide once for it. We do not want to default
99 // construct, or else we might end up using a bad endianness or pointer size!
100 //
101 assert(!TargetName.empty() &&
102 "ERROR: Tool did not specify a target data to use!");
103
Chris Lattner85131c82002-10-14 22:41:13 +0000104 LittleEndian = isLittleEndian;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000105 PointerSize = PtrSize;
106 PointerAlignment = PtrAl;
107 DoubleAlignment = DoubleAl;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000108 assert(DoubleAlignment == PtrAl &&
109 "Double alignment and pointer alignment agree for now!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000110 FloatAlignment = FloatAl;
111 LongAlignment = LongAl;
112 IntAlignment = IntAl;
113 ShortAlignment = ShortAl;
114 ByteAlignment = ByteAl;
115}
116
Chris Lattner53a0c382003-04-24 19:09:05 +0000117TargetData::TargetData(const std::string &ToolName, const Module *M)
118 : AID(AnnotationManager::getID("TargetData::" + ToolName)) {
119 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
120
Chris Lattner030574f2003-08-24 13:49:22 +0000121 LittleEndian = M->getEndianness() != Module::BigEndian;
122 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
Chris Lattner53a0c382003-04-24 19:09:05 +0000123 PointerAlignment = PointerSize;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000124 DoubleAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000125 FloatAlignment = 4;
126 LongAlignment = 8;
127 IntAlignment = 4;
128 ShortAlignment = 2;
129 ByteAlignment = 1;
130}
131
Chris Lattnere7fb3602001-08-27 16:00:15 +0000132TargetData::~TargetData() {
133 AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory
134}
135
136static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +0000137 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000138 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000139 switch (Ty->getPrimitiveID()) {
140 case Type::VoidTyID:
141 case Type::BoolTyID:
142 case Type::UByteTyID:
143 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
144 case Type::UShortTyID:
145 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
146 case Type::UIntTyID:
147 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
148 case Type::ULongTyID:
149 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
150 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
151 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
152 case Type::LabelTyID:
153 case Type::PointerTyID:
154 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
155 return;
156 case Type::ArrayTyID: {
157 const ArrayType *ATy = (const ArrayType *)Ty;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000158 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
159 Size *= ATy->getNumElements();
160 return;
161 }
162 case Type::StructTyID: {
163 // Get the layout annotation... which is lazily created on demand.
164 const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty);
165 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
166 return;
167 }
168
169 case Type::TypeTyID:
170 default:
171 assert(0 && "Bad type for getTypeInfo!!!");
172 return;
173 }
174}
175
Vikram S. Advef66723f2002-05-19 15:28:02 +0000176uint64_t TargetData::getTypeSize(const Type *Ty) const {
177 uint64_t Size;
178 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000179 getTypeInfo(Ty, this, Size, Align);
180 return Size;
181}
182
183unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000184 uint64_t Size;
185 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000186 getTypeInfo(Ty, this, Size, Align);
187 return Align;
188}
189
Chris Lattnerf0453282003-12-22 05:01:15 +0000190/// getIntPtrType - Return an unsigned integer type that is the same size or
191/// greater to the host pointer size.
192const Type *TargetData::getIntPtrType() const {
193 switch (getPointerSize()) {
194 default: assert(0 && "Unknown pointer size!");
195 case 2: return Type::UShortTy;
196 case 4: return Type::UIntTy;
197 case 8: return Type::ULongTy;
198 }
199}
200
201
Vikram S. Advef66723f2002-05-19 15:28:02 +0000202uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Chris Lattner697954c2002-01-20 22:54:45 +0000203 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000204 const Type *Ty = ptrTy;
205 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000206 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000207
Chris Lattner3cac88a2002-09-11 01:21:33 +0000208 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) {
209 if (Idx[CurIDX]->getType() == Type::LongTy) {
Vikram S. Adveca710e92002-08-13 18:17:56 +0000210 // Update Ty to refer to current element
211 Ty = cast<SequentialType>(Ty)->getElementType();
212
Vikram S. Advef66723f2002-05-19 15:28:02 +0000213 // Get the array index and the size of each array element.
Chris Lattner3cac88a2002-09-11 01:21:33 +0000214 int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
Chris Lattner94a51182003-06-04 02:35:35 +0000215 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattner4954f042003-06-02 05:21:06 +0000216 } else {
217 const StructType *STy = cast<StructType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000218 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000219 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000220
221 // Get structure layout information...
222 const StructLayout *Layout = getStructLayout(STy);
223
224 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000225 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000226 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000227
Chris Lattnere7fb3602001-08-27 16:00:15 +0000228 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000229 Ty = STy->getElementType(FieldNo);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000230 }
231 }
232
233 return Result;
234}
Brian Gaeked0fde302003-11-11 22:41:34 +0000235