2010-03-31 06:26:10 +08:00
|
|
|
//===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef CLANG_CODEGEN_CGRECORDLAYOUT_H
|
|
|
|
#define CLANG_CODEGEN_CGRECORDLAYOUT_H
|
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2010-12-01 07:21:46 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2010-03-31 09:09:11 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2010-03-31 08:11:27 +08:00
|
|
|
namespace llvm {
|
2010-04-13 02:14:18 +08:00
|
|
|
class raw_ostream;
|
2010-11-25 03:37:16 +08:00
|
|
|
class StructType;
|
2010-03-31 08:11:27 +08:00
|
|
|
}
|
|
|
|
|
2010-03-31 06:26:10 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace CodeGen {
|
|
|
|
|
2010-04-14 04:58:55 +08:00
|
|
|
/// \brief Helper object for describing how to generate the code for access to a
|
2010-04-13 02:14:18 +08:00
|
|
|
/// bit-field.
|
2010-04-14 04:58:55 +08:00
|
|
|
///
|
|
|
|
/// This structure is intended to describe the "policy" of how the bit-field
|
|
|
|
/// should be accessed, which may be target, language, or ABI dependent.
|
2010-04-06 00:20:44 +08:00
|
|
|
class CGBitFieldInfo {
|
2010-04-14 04:58:55 +08:00
|
|
|
public:
|
|
|
|
/// Descriptor for a single component of a bit-field access. The entire
|
|
|
|
/// bit-field is constituted of a bitwise OR of all of the individual
|
|
|
|
/// components.
|
|
|
|
///
|
|
|
|
/// Each component describes an accessed value, which is how the component
|
|
|
|
/// should be transferred to/from memory, and a target placement, which is how
|
|
|
|
/// that component fits into the constituted bit-field. The pseudo-IR for a
|
|
|
|
/// load is:
|
|
|
|
///
|
|
|
|
/// %0 = gep %base, 0, FieldIndex
|
|
|
|
/// %1 = gep (i8*) %0, FieldByteOffset
|
|
|
|
/// %2 = (i(AccessWidth) *) %1
|
|
|
|
/// %3 = load %2, align AccessAlignment
|
|
|
|
/// %4 = shr %3, FieldBitStart
|
|
|
|
///
|
|
|
|
/// and the composed bit-field is formed as the boolean OR of all accesses,
|
|
|
|
/// masked to TargetBitWidth bits and shifted to TargetBitOffset.
|
|
|
|
struct AccessInfo {
|
|
|
|
/// Offset of the field to load in the LLVM structure, if any.
|
|
|
|
unsigned FieldIndex;
|
|
|
|
|
|
|
|
/// Byte offset from the field address, if any. This should generally be
|
|
|
|
/// unused as the cleanest IR comes from having a well-constructed LLVM type
|
|
|
|
/// with proper GEP instructions, but sometimes its use is required, for
|
|
|
|
/// example if an access is intended to straddle an LLVM field boundary.
|
|
|
|
unsigned FieldByteOffset;
|
|
|
|
|
|
|
|
/// Bit offset in the accessed value to use. The width is implied by \see
|
|
|
|
/// TargetBitWidth.
|
|
|
|
unsigned FieldBitStart;
|
|
|
|
|
|
|
|
/// Bit width of the memory access to perform.
|
|
|
|
unsigned AccessWidth;
|
|
|
|
|
|
|
|
/// The alignment of the memory access, or 0 if the default alignment should
|
|
|
|
/// be used.
|
|
|
|
//
|
|
|
|
// FIXME: Remove use of 0 to encode default, instead have IRgen do the right
|
|
|
|
// thing when it generates the code, if avoiding align directives is
|
|
|
|
// desired.
|
|
|
|
unsigned AccessAlignment;
|
|
|
|
|
|
|
|
/// Offset for the target value.
|
|
|
|
unsigned TargetBitOffset;
|
|
|
|
|
|
|
|
/// Number of bits in the access that are destined for the bit-field.
|
|
|
|
unsigned TargetBitWidth;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// The components to use to access the bit-field. We may need up to three
|
|
|
|
/// separate components to support up to i64 bit-field access (4 + 2 + 1 byte
|
|
|
|
/// accesses).
|
|
|
|
//
|
|
|
|
// FIXME: De-hardcode this, just allocate following the struct.
|
|
|
|
AccessInfo Components[3];
|
|
|
|
|
2010-04-15 13:09:28 +08:00
|
|
|
/// The total size of the bit-field, in bits.
|
|
|
|
unsigned Size;
|
2010-04-06 00:20:44 +08:00
|
|
|
|
2010-04-15 13:09:28 +08:00
|
|
|
/// The number of access components to use.
|
|
|
|
unsigned NumComponents;
|
2010-04-08 10:59:45 +08:00
|
|
|
|
2010-04-15 13:09:28 +08:00
|
|
|
/// Whether the bit-field is signed.
|
2010-04-06 09:07:44 +08:00
|
|
|
bool IsSigned : 1;
|
2010-04-13 02:14:18 +08:00
|
|
|
|
2010-04-15 13:09:28 +08:00
|
|
|
public:
|
2010-04-15 13:09:32 +08:00
|
|
|
CGBitFieldInfo(unsigned Size, unsigned NumComponents, AccessInfo *_Components,
|
|
|
|
bool IsSigned) : Size(Size), NumComponents(NumComponents),
|
|
|
|
IsSigned(IsSigned) {
|
|
|
|
assert(NumComponents <= 3 && "invalid number of components!");
|
|
|
|
for (unsigned i = 0; i != NumComponents; ++i)
|
|
|
|
Components[i] = _Components[i];
|
|
|
|
|
|
|
|
// Check some invariants.
|
|
|
|
unsigned AccessedSize = 0;
|
|
|
|
for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
|
|
|
|
const AccessInfo &AI = getComponent(i);
|
|
|
|
AccessedSize += AI.TargetBitWidth;
|
|
|
|
|
|
|
|
// We shouldn't try to load 0 bits.
|
|
|
|
assert(AI.TargetBitWidth > 0);
|
|
|
|
|
|
|
|
// We can't load more bits than we accessed.
|
|
|
|
assert(AI.FieldBitStart + AI.TargetBitWidth <= AI.AccessWidth);
|
|
|
|
|
|
|
|
// We shouldn't put any bits outside the result size.
|
|
|
|
assert(AI.TargetBitWidth + AI.TargetBitOffset <= Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the total number of target bits matches the total bit-field
|
|
|
|
// size.
|
|
|
|
assert(AccessedSize == Size && "Total size does not match accessed size!");
|
|
|
|
}
|
2010-04-15 13:09:28 +08:00
|
|
|
|
2010-04-14 04:58:55 +08:00
|
|
|
public:
|
2010-04-14 12:07:59 +08:00
|
|
|
/// \brief Check whether this bit-field access is (i.e., should be sign
|
|
|
|
/// extended on loads).
|
2010-04-14 04:58:55 +08:00
|
|
|
bool isSigned() const { return IsSigned; }
|
|
|
|
|
2010-04-14 12:07:59 +08:00
|
|
|
/// \brief Get the size of the bit-field, in bits.
|
|
|
|
unsigned getSize() const { return Size; }
|
|
|
|
|
|
|
|
/// @name Component Access
|
|
|
|
/// @{
|
|
|
|
|
2010-04-14 04:58:55 +08:00
|
|
|
unsigned getNumComponents() const { return NumComponents; }
|
|
|
|
|
|
|
|
const AccessInfo &getComponent(unsigned Index) const {
|
|
|
|
assert(Index < getNumComponents() && "Invalid access!");
|
|
|
|
return Components[Index];
|
|
|
|
}
|
|
|
|
|
2010-04-14 12:07:59 +08:00
|
|
|
/// @}
|
|
|
|
|
2010-04-13 02:14:18 +08:00
|
|
|
void print(llvm::raw_ostream &OS) const;
|
|
|
|
void dump() const;
|
2010-09-03 07:53:28 +08:00
|
|
|
|
|
|
|
/// \brief Given a bit-field decl, build an appropriate helper object for
|
|
|
|
/// accessing that field (which is expected to have the given offset and
|
|
|
|
/// size).
|
|
|
|
static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, const FieldDecl *FD,
|
|
|
|
uint64_t FieldOffset, uint64_t FieldSize);
|
|
|
|
|
|
|
|
/// \brief Given a bit-field decl, build an appropriate helper object for
|
|
|
|
/// accessing that field (which is expected to have the given offset and
|
|
|
|
/// size). The field decl should be known to be contained within a type of at
|
|
|
|
/// least the given size and with the given alignment.
|
|
|
|
static CGBitFieldInfo MakeInfo(CodeGenTypes &Types, const FieldDecl *FD,
|
|
|
|
uint64_t FieldOffset, uint64_t FieldSize,
|
|
|
|
uint64_t ContainingTypeSizeInBits,
|
|
|
|
unsigned ContainingTypeAlign);
|
2010-04-06 00:20:44 +08:00
|
|
|
};
|
|
|
|
|
2010-03-31 06:26:10 +08:00
|
|
|
/// CGRecordLayout - This class handles struct and union layout info while
|
|
|
|
/// lowering AST types to LLVM types.
|
2010-03-31 09:09:11 +08:00
|
|
|
///
|
|
|
|
/// These layout objects are only created on demand as IR generation requires.
|
2010-03-31 06:26:10 +08:00
|
|
|
class CGRecordLayout {
|
2010-03-31 09:09:11 +08:00
|
|
|
friend class CodeGenTypes;
|
|
|
|
|
2010-03-31 06:26:10 +08:00
|
|
|
CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT
|
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
private:
|
2010-11-09 13:25:47 +08:00
|
|
|
/// The LLVM type corresponding to this record layout.
|
2010-12-01 07:21:46 +08:00
|
|
|
llvm::PATypeHolder LLVMType;
|
2010-03-31 06:26:10 +08:00
|
|
|
|
2010-11-09 13:25:47 +08:00
|
|
|
/// The LLVM type for the non-virtual part of this record layout, used for
|
|
|
|
/// laying out the record as a base.
|
2010-12-01 07:21:46 +08:00
|
|
|
llvm::PATypeHolder NonVirtualBaseLLVMType;
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
/// Map from (non-bit-field) struct field to the corresponding llvm struct
|
|
|
|
/// type field no. This info is populated by record builder.
|
|
|
|
llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
|
|
|
|
|
|
|
|
/// Map from (bit-field) struct field to the corresponding llvm struct type
|
|
|
|
/// field no. This info is populated by record builder.
|
2010-04-06 00:20:44 +08:00
|
|
|
llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
|
2010-03-31 09:09:11 +08:00
|
|
|
|
2010-05-18 13:22:06 +08:00
|
|
|
// FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
|
|
|
|
// map for both virtual and non virtual bases.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBaseFields;
|
|
|
|
|
2010-03-31 06:26:10 +08:00
|
|
|
/// Whether one of the fields in this record layout is a pointer to data
|
|
|
|
/// member, or a struct that contains pointer to data member.
|
2010-08-23 05:01:12 +08:00
|
|
|
bool IsZeroInitializable : 1;
|
2010-03-31 06:26:10 +08:00
|
|
|
|
|
|
|
public:
|
2010-11-25 03:37:16 +08:00
|
|
|
CGRecordLayout(const llvm::StructType *LLVMType,
|
|
|
|
const llvm::StructType *NonVirtualBaseLLVMType,
|
2010-11-09 13:25:47 +08:00
|
|
|
bool IsZeroInitializable)
|
2010-11-22 07:59:45 +08:00
|
|
|
: LLVMType(LLVMType), NonVirtualBaseLLVMType(NonVirtualBaseLLVMType),
|
2010-11-09 13:25:47 +08:00
|
|
|
IsZeroInitializable(IsZeroInitializable) {}
|
2010-03-31 06:26:10 +08:00
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
/// \brief Return the LLVM type associated with this record.
|
2010-11-25 03:37:16 +08:00
|
|
|
const llvm::StructType *getLLVMType() const {
|
2010-12-01 07:21:46 +08:00
|
|
|
return cast<llvm::StructType>(LLVMType.get());
|
2010-03-31 06:26:10 +08:00
|
|
|
}
|
|
|
|
|
2010-11-25 03:37:16 +08:00
|
|
|
const llvm::StructType *getNonVirtualBaseLLVMType() const {
|
2010-12-01 07:21:46 +08:00
|
|
|
return cast<llvm::StructType>(NonVirtualBaseLLVMType.get());
|
2010-11-09 13:25:47 +08:00
|
|
|
}
|
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
/// \brief Check whether this struct can be C++ zero-initialized
|
|
|
|
/// with a zeroinitializer.
|
|
|
|
bool isZeroInitializable() const {
|
|
|
|
return IsZeroInitializable;
|
2010-03-31 06:26:10 +08:00
|
|
|
}
|
2010-03-31 09:09:11 +08:00
|
|
|
|
2010-04-27 22:51:07 +08:00
|
|
|
/// \brief Return llvm::StructType element number that corresponds to the
|
|
|
|
/// field FD.
|
2010-03-31 09:09:11 +08:00
|
|
|
unsigned getLLVMFieldNo(const FieldDecl *FD) const {
|
|
|
|
assert(!FD->isBitField() && "Invalid call for bit-field decl!");
|
|
|
|
assert(FieldInfo.count(FD) && "Invalid field for record!");
|
|
|
|
return FieldInfo.lookup(FD);
|
|
|
|
}
|
|
|
|
|
2010-05-18 13:22:06 +08:00
|
|
|
unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
|
|
|
|
assert(NonVirtualBaseFields.count(RD) && "Invalid non-virtual base!");
|
|
|
|
return NonVirtualBaseFields.lookup(RD);
|
|
|
|
}
|
|
|
|
|
2010-04-27 22:51:07 +08:00
|
|
|
/// \brief Return the BitFieldInfo that corresponds to the field FD.
|
2010-04-06 00:20:44 +08:00
|
|
|
const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
|
2010-03-31 09:09:11 +08:00
|
|
|
assert(FD->isBitField() && "Invalid call for non bit-field decl!");
|
2010-04-06 00:20:44 +08:00
|
|
|
llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
|
2010-03-31 09:09:11 +08:00
|
|
|
it = BitFields.find(FD);
|
|
|
|
assert(it != BitFields.end() && "Unable to find bitfield info");
|
|
|
|
return it->second;
|
|
|
|
}
|
2010-04-13 02:14:18 +08:00
|
|
|
|
|
|
|
void print(llvm::raw_ostream &OS) const;
|
|
|
|
void dump() const;
|
2010-03-31 06:26:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace CodeGen
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|