1 #include <magic/support/BitFieldAggregation.h>
9 #define BitFieldAggregation_assert_or_return(R,RV,T,ET,X) do { \
12 errs() << "Assertion failed, dumping types...\n"; \
13 errs() << TypeUtil::getDescription(T, ET); \
18 //===----------------------------------------------------------------------===//
19 // Constructors, destructor, and operators
20 //===----------------------------------------------------------------------===//
22 BitFieldAggregation::BitFieldAggregation(TYPECONST Type
* type
, std::vector
<EDIType
> EDITypes
, unsigned typeIndex
, unsigned EDITypeIndex
, std::vector
<DIDerivedType
> members
, unsigned counter
) {
23 init(type
, EDITypes
, typeIndex
, EDITypeIndex
, members
, counter
);
26 BitFieldAggregation::BitFieldAggregation() {
33 void BitFieldAggregation::init(TYPECONST Type
* type
, std::vector
<EDIType
> EDITypes
, unsigned typeIndex
, unsigned EDITypeIndex
, std::vector
<DIDerivedType
> members
, unsigned counter
) {
34 assert(members
.size() == EDITypes
.size());
35 assert(typeIndex
<= EDITypeIndex
);
36 size
= members
.size();
40 this->EDITypes
= EDITypes
;
41 this->typeIndex
= typeIndex
;
42 this->EDITypeIndex
= EDITypeIndex
;
43 this->members
= members
;
44 raw_string_ostream
ostream(name
);
45 ostream
<< bfaNamePrefix
<< counter
;
49 //===----------------------------------------------------------------------===//
51 //===----------------------------------------------------------------------===//
53 const std::string
BitFieldAggregation::getDescription() const {
55 raw_string_ostream
ostream(string
);
56 ostream
<< "[\nname = " << name
<< "\nmembers = ";
57 for(unsigned i
=0;i
<size
;i
++) {
58 ostream
<< (i
==0 ? "" : ", ") << members
[i
].getName();
60 ostream
<< "\ntype = \n" << TypeUtil::getDescription(getType());
61 std::vector
<EDIType
> EDITypes
= getEDITypes();
62 for(unsigned i
=0;i
<EDITypes
.size();i
++) {
63 ostream
<< "\nEDIType" << i
<< " =\n" << TypeUtil::getDescription(&EDITypes
[i
]);
70 //===----------------------------------------------------------------------===//
71 // Public static methods
72 //===----------------------------------------------------------------------===//
74 bool BitFieldAggregation::getBitFieldAggregations(TYPECONST Type
*type
, const EDIType
*aEDIType
, std::vector
<BitFieldAggregation
> &bfas
, bool returnOnError
) {
75 std::vector
<BitFieldAggregation
> emptyBfas
;
76 if(!hasBitFields(type
, aEDIType
)) {
79 unsigned typeIndex
= 0;
80 unsigned EDITypeIndex
= 0;
81 unsigned typeContainedTypes
= type
->getNumContainedTypes();
82 unsigned aEDITypeContainedTypes
= aEDIType
->getNumContainedTypes();
84 const EDIType
privateEDIType(*aEDIType
);
85 aEDIType
= &privateEDIType
;
86 while(typeIndex
< typeContainedTypes
) {
87 TYPECONST Type
*containedType
= type
->getContainedType(typeIndex
);
88 if(EDITypeIndex
>= aEDITypeContainedTypes
&& typeIndex
== typeContainedTypes
-1 && TypeUtil::isPaddedType(type
)) {
91 BitFieldAggregation_assert_or_return(returnOnError
, false, type
, aEDIType
, EDITypeIndex
< aEDITypeContainedTypes
);
92 const EDIType containedEDIType
= aEDIType
->getContainedType(EDITypeIndex
);
93 unsigned typeBits
= TypeUtil::typeToBits(containedType
);
94 if(typeBits
> 0 && containedEDIType
.isIntegerTy()) {
95 unsigned EDITypeBits
= aEDIType
->getMember(EDITypeIndex
).getSizeInBits();
96 assert(typeBits
>= EDITypeBits
);
97 if(typeBits
> EDITypeBits
) {
98 unsigned lastTypeIndex
= typeIndex
;
99 unsigned lastEDITypeIndex
= EDITypeIndex
;
100 while(lastEDITypeIndex
+1 < aEDITypeContainedTypes
&& isBitField(type
, aEDIType
, lastEDITypeIndex
+1)) { // grab all the bitfields following the first one found
102 EDITypeBits
+= aEDIType
->getMember(lastEDITypeIndex
).getSizeInBits();
104 while(lastTypeIndex
+1 < typeContainedTypes
&& EDITypeBits
> typeBits
) { // grab all the necessary fields to cover all the bits found in the bitfields
106 typeBits
+= TypeUtil::typeToBits(type
->getContainedType(lastTypeIndex
));
108 BitFieldAggregation
*bfa
= BitFieldAggregation::getBitFieldAggregation(type
, aEDIType
, returnOnError
, typeIndex
, EDITypeIndex
, lastTypeIndex
, lastEDITypeIndex
, counter
++);
109 BitFieldAggregation_assert_or_return(returnOnError
, false, type
, aEDIType
, bfa
!= NULL
);
110 if(bfa
->getSize() > 1) {
111 //we don't care about single-element aggregates
112 bfas
.push_back(*bfa
);
115 EDITypeIndex
+= bfa
->getSize();
125 BitFieldAggregation
*BitFieldAggregation::getBitFieldAggregation(TYPECONST Type
*type
, const EDIType
*aEDIType
, bool returnOnError
, unsigned typeIndex
, unsigned EDITypeIndex
, unsigned lastTypeIndex
, unsigned lastEDITypeIndex
, unsigned counter
) {
126 static BitFieldAggregation bfa
;
127 TYPECONST Type
*containedType
= type
->getContainedType(typeIndex
);
128 unsigned typeBits
= TypeUtil::typeToBits(containedType
);
129 assert(typeBits
> 0);
130 unsigned nextTypeBits
= 0;
131 if (typeIndex
< lastTypeIndex
) {
132 nextTypeBits
= TypeUtil::typeToBits(type
->getContainedType(typeIndex
+1));
134 const int maxNumMembers
= (lastEDITypeIndex
- EDITypeIndex
) - (lastTypeIndex
- typeIndex
) + 1;
135 unsigned index
= EDITypeIndex
;
136 std::vector
<DIDerivedType
> members
;
137 std::vector
<EDIType
> containedEDITypes
;
139 BitFieldAggregation_assert_or_return(returnOnError
, NULL
, type
, aEDIType
, maxNumMembers
> 0);
141 BitFieldAggregationErr("getBitFieldAggregation(): typeIndex = " << typeIndex
<< ", EDITypeIndex = " << EDITypeIndex
<< ", maxNumMembers = " << maxNumMembers
);
142 BitFieldAggregationErr("getBitFieldAggregation(): lastTypeIndex = " << lastTypeIndex
<< ", lastEDITypeIndex = " << lastEDITypeIndex
);
143 BitFieldAggregationErr("getBitFieldAggregation(): " << TypeUtil::getDescription(type
) << " VS " << TypeUtil::getDescription(aEDIType
));
145 while(index
<= lastEDITypeIndex
&& members
.size() < (unsigned)maxNumMembers
) {
146 const EDIType containedEDIType
= aEDIType
->getContainedType(index
);
148 BitFieldAggregationErr("Examining type " << TypeUtil::getDescription(&containedEDIType
));
150 BitFieldAggregation_assert_or_return(returnOnError
, NULL
, type
, aEDIType
, containedEDIType
.isIntegerTy());
151 DIDerivedType member
= aEDIType
->getMember(index
);
152 unsigned EDITypeBits
= member
.getSizeInBits();
154 BitFieldAggregationErr("Type bits = " << typeBits
<< ", next type bits = " << nextTypeBits
<< ", index = " << index
);
155 BitFieldAggregationErr("This is member " << member
.getName() << " with bits " << EDITypeBits
);
157 if((index
> EDITypeIndex
&& EDITypeBits
== nextTypeBits
) || EDITypeBits
> typeBits
) {
160 typeBits
-= EDITypeBits
;
161 members
.push_back(member
);
162 containedEDITypes
.push_back(containedEDIType
);
165 bfa
.init(containedType
, containedEDITypes
, typeIndex
, EDITypeIndex
, members
, counter
);
169 std::string
BitFieldAggregation::bfaNamePrefix
= BFA_NAME_PREFIX
;