Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef CLANG_CODEGEN_CGRECORDLAYOUT_H |
| 11 | #define CLANG_CODEGEN_CGRECORDLAYOUT_H |
| 12 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 15 | namespace llvm { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 16 | class raw_ostream; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 17 | class Type; |
| 18 | } |
| 19 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 20 | namespace clang { |
| 21 | namespace CodeGen { |
| 22 | |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 23 | /// \brief Helper object for describing how to generate the code for access to a |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 24 | /// bit-field. |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 25 | /// |
| 26 | /// This structure is intended to describe the "policy" of how the bit-field |
| 27 | /// should be accessed, which may be target, language, or ABI dependent. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 28 | class CGBitFieldInfo { |
| 29 | public: |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 30 | /// Descriptor for a single component of a bit-field access. The entire |
| 31 | /// bit-field is constituted of a bitwise OR of all of the individual |
| 32 | /// components. |
| 33 | /// |
| 34 | /// Each component describes an accessed value, which is how the component |
| 35 | /// should be transferred to/from memory, and a target placement, which is how |
| 36 | /// that component fits into the constituted bit-field. The pseudo-IR for a |
| 37 | /// load is: |
| 38 | /// |
| 39 | /// %0 = gep %base, 0, FieldIndex |
| 40 | /// %1 = gep (i8*) %0, FieldByteOffset |
| 41 | /// %2 = (i(AccessWidth) *) %1 |
| 42 | /// %3 = load %2, align AccessAlignment |
| 43 | /// %4 = shr %3, FieldBitStart |
| 44 | /// |
| 45 | /// and the composed bit-field is formed as the boolean OR of all accesses, |
| 46 | /// masked to TargetBitWidth bits and shifted to TargetBitOffset. |
| 47 | struct AccessInfo { |
| 48 | /// Offset of the field to load in the LLVM structure, if any. |
| 49 | unsigned FieldIndex; |
| 50 | |
| 51 | /// Byte offset from the field address, if any. This should generally be |
| 52 | /// unused as the cleanest IR comes from having a well-constructed LLVM type |
| 53 | /// with proper GEP instructions, but sometimes its use is required, for |
| 54 | /// example if an access is intended to straddle an LLVM field boundary. |
| 55 | unsigned FieldByteOffset; |
| 56 | |
| 57 | /// Bit offset in the accessed value to use. The width is implied by \see |
| 58 | /// TargetBitWidth. |
| 59 | unsigned FieldBitStart; |
| 60 | |
| 61 | /// Bit width of the memory access to perform. |
| 62 | unsigned AccessWidth; |
| 63 | |
| 64 | /// The alignment of the memory access, or 0 if the default alignment should |
| 65 | /// be used. |
| 66 | // |
| 67 | // FIXME: Remove use of 0 to encode default, instead have IRgen do the right |
| 68 | // thing when it generates the code, if avoiding align directives is |
| 69 | // desired. |
| 70 | unsigned AccessAlignment; |
| 71 | |
| 72 | /// Offset for the target value. |
| 73 | unsigned TargetBitOffset; |
| 74 | |
| 75 | /// Number of bits in the access that are destined for the bit-field. |
| 76 | unsigned TargetBitWidth; |
| 77 | }; |
| 78 | |
| 79 | private: |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 80 | /// The components to use to access the bit-field. We may need up to three |
| 81 | /// separate components to support up to i64 bit-field access (4 + 2 + 1 byte |
| 82 | /// accesses). |
| 83 | // |
| 84 | // FIXME: De-hardcode this, just allocate following the struct. |
| 85 | AccessInfo Components[3]; |
| 86 | |
Daniel Dunbar | bb13845 | 2010-04-15 05:09:28 +0000 | [diff] [blame^] | 87 | /// The total size of the bit-field, in bits. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 88 | unsigned Size; |
Daniel Dunbar | bb13845 | 2010-04-15 05:09:28 +0000 | [diff] [blame^] | 89 | |
| 90 | /// The number of access components to use. |
| 91 | unsigned NumComponents; |
| 92 | |
| 93 | /// Whether the bit-field is signed. |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 94 | bool IsSigned : 1; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 96 | public: |
Daniel Dunbar | bb13845 | 2010-04-15 05:09:28 +0000 | [diff] [blame^] | 97 | CGBitFieldInfo(unsigned Size, bool IsSigned) |
| 98 | : Size(Size), IsSigned(IsSigned) {} |
| 99 | |
| 100 | public: |
Daniel Dunbar | b2b40a4 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 101 | /// \brief Check whether this bit-field access is (i.e., should be sign |
| 102 | /// extended on loads). |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 103 | bool isSigned() const { return IsSigned; } |
| 104 | |
Daniel Dunbar | b2b40a4 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 105 | /// \brief Get the size of the bit-field, in bits. |
| 106 | unsigned getSize() const { return Size; } |
| 107 | |
| 108 | /// @name Component Access |
| 109 | /// @{ |
| 110 | |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 111 | unsigned getNumComponents() const { return NumComponents; } |
| 112 | void setNumComponents(unsigned Value) { |
| 113 | assert(Value < 4 && "Invalid number of components!"); |
| 114 | NumComponents = Value; |
| 115 | } |
| 116 | |
| 117 | const AccessInfo &getComponent(unsigned Index) const { |
| 118 | assert(Index < getNumComponents() && "Invalid access!"); |
| 119 | return Components[Index]; |
| 120 | } |
| 121 | AccessInfo &getComponent(unsigned Index) { |
| 122 | assert(Index < getNumComponents() && "Invalid access!"); |
| 123 | return Components[Index]; |
| 124 | } |
| 125 | |
Daniel Dunbar | b2b40a4 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 126 | /// @} |
| 127 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 128 | void print(llvm::raw_ostream &OS) const; |
| 129 | void dump() const; |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 132 | /// CGRecordLayout - This class handles struct and union layout info while |
| 133 | /// lowering AST types to LLVM types. |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 134 | /// |
| 135 | /// These layout objects are only created on demand as IR generation requires. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 136 | class CGRecordLayout { |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 137 | friend class CodeGenTypes; |
| 138 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 139 | CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT |
| 140 | void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT |
| 141 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 142 | private: |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 143 | /// The LLVMType corresponding to this record layout. |
| 144 | const llvm::Type *LLVMType; |
| 145 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 146 | /// Map from (non-bit-field) struct field to the corresponding llvm struct |
| 147 | /// type field no. This info is populated by record builder. |
| 148 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 149 | |
| 150 | /// Map from (bit-field) struct field to the corresponding llvm struct type |
| 151 | /// field no. This info is populated by record builder. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 152 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 154 | /// Whether one of the fields in this record layout is a pointer to data |
| 155 | /// member, or a struct that contains pointer to data member. |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 156 | bool ContainsPointerToDataMember : 1; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 157 | |
| 158 | public: |
| 159 | CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember) |
| 160 | : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) {} |
| 161 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 162 | /// \brief Return the LLVM type associated with this record. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 163 | const llvm::Type *getLLVMType() const { |
| 164 | return LLVMType; |
| 165 | } |
| 166 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 167 | /// \brief Check whether this struct contains pointers to data members. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 168 | bool containsPointerToDataMember() const { |
| 169 | return ContainsPointerToDataMember; |
| 170 | } |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 171 | |
| 172 | /// \brief Return the BitFieldInfo that corresponds to the field FD. |
| 173 | unsigned getLLVMFieldNo(const FieldDecl *FD) const { |
| 174 | assert(!FD->isBitField() && "Invalid call for bit-field decl!"); |
| 175 | assert(FieldInfo.count(FD) && "Invalid field for record!"); |
| 176 | return FieldInfo.lookup(FD); |
| 177 | } |
| 178 | |
| 179 | /// \brief Return llvm::StructType element number that corresponds to the |
| 180 | /// field FD. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 181 | const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 182 | assert(FD->isBitField() && "Invalid call for non bit-field decl!"); |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 183 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 184 | it = BitFields.find(FD); |
| 185 | assert(it != BitFields.end() && "Unable to find bitfield info"); |
| 186 | return it->second; |
| 187 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 188 | |
| 189 | void print(llvm::raw_ostream &OS) const; |
| 190 | void dump() const; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // end namespace CodeGen |
| 194 | } // end namespace clang |
| 195 | |
| 196 | #endif |