1 #ifndef BIT_FIELD_AGGREGATION_H
2 #define BIT_FIELD_AGGREGATION_H
4 #include <magic/support/EDIType.h>
5 #include <magic/support/TypeUtil.h>
11 #define BitFieldAggregationErr(M) errs() << "BitFieldAggregation: " << M << "\n"
13 #define BFA_NAME_PREFIX "__BFA__"
15 class BitFieldAggregation
{
17 BitFieldAggregation(TYPECONST Type
* type
, std::vector
<EDIType
> EDITypes
, unsigned typeIndex
, unsigned EDITypeIndex
, std::vector
<DIDerivedType
> members
, unsigned counter
);
18 BitFieldAggregation();
19 void init(TYPECONST Type
* type
, std::vector
<EDIType
> EDITypes
, unsigned typeIndex
, unsigned EDITypeIndex
, std::vector
<DIDerivedType
> members
, unsigned counter
);
21 const std::string
getDescription() const;
23 unsigned getTypeIndex() const;
24 unsigned getEDITypeIndex() const;
25 std::string
getName() const;
26 std::vector
<DIDerivedType
> getMembers() const;
28 unsigned getSize() const;
29 TYPECONST Type
*getType() const;
30 std::vector
<EDIType
> getEDITypes() const;
31 unsigned getRepresentativeEDITypeIndex() const;
33 void print(raw_ostream
&OS
) const;
35 static bool getBitFieldAggregations(TYPECONST Type
*type
, const EDIType
*aEDIType
, std::vector
<BitFieldAggregation
> &bfas
, bool returnOnError
=false);
36 static bool hasBitFields(TYPECONST Type
*type
, const EDIType
*aEDIType
);
37 static bool isBitField(TYPECONST Type
*type
, const EDIType
*aEDIType
, unsigned memberIdx
);
41 std::vector
<EDIType
> EDITypes
;
43 unsigned EDITypeIndex
;
45 std::vector
<DIDerivedType
> members
;
48 static std::string bfaNamePrefix
;
50 static BitFieldAggregation
* getBitFieldAggregation(TYPECONST Type
*type
, const EDIType
*aEDIType
, bool returnOnError
, unsigned typeIndex
, unsigned EDITypeIndex
, unsigned lastTypeIndex
, unsigned lastEDITypeIndex
, unsigned counter
);
53 inline raw_ostream
&operator<<(raw_ostream
&OS
, const BitFieldAggregation
&bfa
) {
58 inline unsigned BitFieldAggregation::getTypeIndex() const {
62 inline unsigned BitFieldAggregation::getEDITypeIndex() const {
66 inline std::string
BitFieldAggregation::getName() const {
70 inline std::vector
<DIDerivedType
> BitFieldAggregation::getMembers() const {
74 inline unsigned BitFieldAggregation::getSize() const {
78 inline TYPECONST Type
*BitFieldAggregation::getType() const {
82 inline std::vector
<EDIType
> BitFieldAggregation::getEDITypes() const {
86 inline unsigned BitFieldAggregation::getRepresentativeEDITypeIndex() const {
90 inline void BitFieldAggregation::print(raw_ostream
&OS
) const {
91 OS
<< getDescription();
94 inline bool BitFieldAggregation::hasBitFields(TYPECONST Type
*type
, const EDIType
*aEDIType
) {
95 if(!aEDIType
->isStructTy()) {
98 unsigned numContainedTypes
= aEDIType
->getNumContainedTypes();
99 for(unsigned i
=0;i
<numContainedTypes
;i
++) {
100 if (isBitField(type
, aEDIType
, i
)) {
107 inline bool BitFieldAggregation::isBitField(TYPECONST Type
*type
, const EDIType
*aEDIType
, unsigned memberIdx
) {
108 const DIDerivedType subDIType
= aEDIType
->getMember(memberIdx
);
109 unsigned EDITypeBits
= subDIType
.getSizeInBits();
110 const DIType aDIType
= PassUtil::getDITypeDerivedFrom(subDIType
);
111 unsigned EDITypeOriginalBits
= aDIType
.getSizeInBits();
112 return (EDITypeBits
>0 && EDITypeOriginalBits
>0 && EDITypeBits
!= EDITypeOriginalBits
);