blob: 0d23206d991a7884304a248cb6b54f01c673ab7d [file] [log] [blame]
Chris Lattnere7fb3602001-08-27 16:00:15 +00001//===-- 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
13#include "llvm/CodeGen/TargetData.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/ConstPoolVals.h"
16
17static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
18 unsigned &Size, unsigned char &Alignment);
19
20//===----------------------------------------------------------------------===//
21// Support for StructLayout Annotation
22//===----------------------------------------------------------------------===//
23
24StructLayout::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;
35 unsigned TySize, TyAlign;
36 getTypeInfo(Ty, &TD, TySize, A); TyAlign = A;
37
38 // Add padding if neccesary to make the data element aligned properly...
39 if (StructSize % TyAlign != 0)
40 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
41
42 // Keep track of maximum alignment constraint
43 StructAlignment = max(TyAlign, StructAlignment);
44
45 MemberOffsets.push_back(StructSize);
46 StructSize += TySize; // Consume space for this data item...
47 }
48
49 // Add padding to the end of the struct so that it could be put in an array
50 // and all array elements would be aligned correctly.
51 if (StructSize % StructAlignment != 0)
52 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
53
54 if (StructSize == 0) {
55 StructSize = 1; // Empty struct is 1 byte
56 StructAlignment = 1;
57 }
58}
59
60Annotation *TargetData::TypeAnFactory(AnnotationID AID, Annotable *T, void *D) {
61 const TargetData &TD = *(const TargetData*)D;
62 assert(AID == TD.AID && "Target data annotation ID mismatch!");
63 const Type *Ty = ((const Value *)T)->castTypeAsserting();
64 assert(Ty->isStructType() &&
65 "Can only create StructLayout annotation on structs!");
66 return new StructLayout((const StructType *)Ty, TD);
67}
68
69//===----------------------------------------------------------------------===//
70// TargetData Class Implementation
71//===----------------------------------------------------------------------===//
72
73TargetData::TargetData(const string &TargetName, unsigned char PtrSize = 8,
74 unsigned char PtrAl = 8, unsigned char DoubleAl = 8,
75 unsigned char FloatAl = 4, unsigned char LongAl = 8,
76 unsigned char IntAl = 4, unsigned char ShortAl = 2,
77 unsigned char ByteAl = 1)
78 : AID(AnnotationManager::getID("TargetData::" + TargetName)) {
79 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
80
81 PointerSize = PtrSize;
82 PointerAlignment = PtrAl;
83 DoubleAlignment = DoubleAl;
84 FloatAlignment = FloatAl;
85 LongAlignment = LongAl;
86 IntAlignment = IntAl;
87 ShortAlignment = ShortAl;
88 ByteAlignment = ByteAl;
89}
90
91TargetData::~TargetData() {
92 AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory
93}
94
95static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
96 unsigned &Size, unsigned char &Alignment) {
97 switch (Ty->getPrimitiveID()) {
98 case Type::VoidTyID:
99 case Type::BoolTyID:
100 case Type::UByteTyID:
101 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
102 case Type::UShortTyID:
103 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
104 case Type::UIntTyID:
105 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
106 case Type::ULongTyID:
107 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
108 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
109 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
110 case Type::LabelTyID:
111 case Type::PointerTyID:
112 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
113 return;
114 case Type::ArrayTyID: {
115 const ArrayType *ATy = (const ArrayType *)Ty;
116 assert(ATy->isSized() && "Can't get TypeInfo of an unsized array!");
117 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
118 Size *= ATy->getNumElements();
119 return;
120 }
121 case Type::StructTyID: {
122 // Get the layout annotation... which is lazily created on demand.
123 const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty);
124 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
125 return;
126 }
127
128 case Type::TypeTyID:
129 default:
130 assert(0 && "Bad type for getTypeInfo!!!");
131 return;
132 }
133}
134
135unsigned TargetData::getTypeSize(const Type *Ty) const {
136 unsigned Size; unsigned char Align;
137 getTypeInfo(Ty, this, Size, Align);
138 return Size;
139}
140
141unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
142 unsigned Size; unsigned char Align;
143 getTypeInfo(Ty, this, Size, Align);
144 return Align;
145}
146
147unsigned TargetData::getIndexedOffset(const Type *ptrTy,
148 const vector<ConstPoolVal*> &Idx) const {
Chris Lattner9ff64a82001-08-27 18:54:45 +0000149 const PointerType *PtrTy = ptrTy->isPointerType(); // Returns null if not
Chris Lattnere7fb3602001-08-27 16:00:15 +0000150 assert(PtrTy && "getIndexedOffset on nonpointer!");
151
152 unsigned Result = 0;
153
154 // Get the type pointed to...
155 const Type *Ty = PtrTy->getValueType();
156
157 for (unsigned CurIDX = 0; CurIDX < Idx.size(); ++CurIDX) {
158 if (const StructType *STy = Ty->isStructType()) {
159 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
160 unsigned FieldNo = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
161
162 // Get structure layout information...
163 const StructLayout *Layout = getStructLayout(STy);
164
165 // Add in the offset, as calculated by the structure layout info...
166 assert(FieldNo < Layout->MemberOffsets.size() && "FieldNo out of range!");
167 Result += Layout->MemberOffsets[FieldNo];
168
169 // Update Ty to refer to current element
170 Ty = STy->getElementTypes()[FieldNo];
171
172 } else if (const ArrayType *ATy = Ty->isArrayType()) {
173 assert(0 && "Loading from arrays not implemented yet!");
174 } else {
175 assert(0 && "Indexing type that is not struct or array?");
176 return 0; // Load directly through ptr
177 }
178 }
179
180 return Result;
181}