Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.h - Record builder helper --------*- 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 | // This is a helper class used to build CGRecordLayout objects and LLVM types. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CLANG_CODEGEN_CGRECORDLAYOUTBUILDER_H |
| 15 | #define CLANG_CODEGEN_CGRECORDLAYOUTBUILDER_H |
| 16 | |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/Support/DataTypes.h" |
| 19 | #include <vector> |
| 20 | |
| 21 | namespace llvm { |
| 22 | class Type; |
| 23 | } |
| 24 | |
| 25 | namespace clang { |
| 26 | class FieldDecl; |
| 27 | class RecordDecl; |
| 28 | |
| 29 | namespace CodeGen { |
| 30 | class CGRecordLayout; |
| 31 | class CodeGenTypes; |
| 32 | |
| 33 | class CGRecordLayoutBuilder { |
| 34 | CodeGenTypes &Types; |
| 35 | |
| 36 | /// StructPacking - Will be 0 if this struct is not packed. If it is packed, |
| 37 | /// it will have the packing alignment in bits. |
| 38 | /// |
| 39 | unsigned StructPacking; |
| 40 | |
| 41 | /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the |
| 42 | /// LLVM types. |
| 43 | unsigned AlignmentAsLLVMStruct; |
| 44 | |
| 45 | /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field, |
| 46 | /// this will have the number of bits still available in the field. |
| 47 | char BitsAvailableInLastField; |
| 48 | |
| 49 | /// FieldTypes - Holds the LLVM types that the struct is created from. |
| 50 | std::vector<const llvm::Type *> FieldTypes; |
| 51 | |
| 52 | /// FieldInfo - Holds size and offset information about a field. |
| 53 | /// FIXME: I think we can get rid of this. |
| 54 | struct FieldInfo { |
| 55 | FieldInfo(uint64_t OffsetInBytes, uint64_t SizeInBytes) |
| 56 | : OffsetInBytes(OffsetInBytes), SizeInBytes(SizeInBytes) { } |
| 57 | |
| 58 | const uint64_t OffsetInBytes; |
| 59 | const uint64_t SizeInBytes; |
| 60 | }; |
| 61 | llvm::SmallVector<FieldInfo, 16> FieldInfos; |
| 62 | |
| 63 | /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number. |
| 64 | typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo; |
| 65 | llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields; |
| 66 | |
| 67 | /// LLVMBitFieldInfo - Holds location and size information about a bit field. |
| 68 | struct LLVMBitFieldInfo { |
Anders Carlsson | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame^] | 69 | LLVMBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, unsigned Start, |
| 70 | unsigned Size) |
| 71 | : FD(FD), FieldNo(FieldNo), Start(Start), Size(Size) { } |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 72 | |
| 73 | const FieldDecl *FD; |
| 74 | |
Anders Carlsson | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame^] | 75 | unsigned FieldNo; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 76 | unsigned Start; |
| 77 | unsigned Size; |
| 78 | }; |
| 79 | llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields; |
| 80 | |
| 81 | CGRecordLayoutBuilder(CodeGenTypes &Types) |
| 82 | : Types(Types), StructPacking(0), AlignmentAsLLVMStruct(1) |
| 83 | , BitsAvailableInLastField(0) { } |
| 84 | |
| 85 | /// Layout - Will layout a RecordDecl. |
| 86 | void Layout(const RecordDecl *D); |
| 87 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 88 | /// LayoutUnion - Will layout a union RecordDecl. |
| 89 | void LayoutUnion(const RecordDecl *D); |
| 90 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 91 | /// LayoutField - try to layout all fields in the record decl. |
| 92 | /// Returns false if the operation failed because the struct is not packed. |
| 93 | bool LayoutFields(const RecordDecl *D); |
| 94 | |
| 95 | /// LayoutField - layout a single field. Returns false if the operation failed |
| 96 | /// because the current struct is not packed. |
| 97 | bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); |
| 98 | |
| 99 | /// LayoutBitField - layout a single bit field. |
| 100 | void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); |
| 101 | |
| 102 | /// AppendField - Appends a field with the given offset size and type. |
| 103 | void AppendField(uint64_t FieldOffsetInBytes, uint64_t FieldSizeInBytes, |
| 104 | const llvm::Type *FieldTy); |
| 105 | |
| 106 | /// AppendPadding - Appends enough padding bytes so that the total struct |
| 107 | /// size matches the alignment of the passed in type. |
| 108 | void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy); |
| 109 | |
| 110 | /// AppendPadding - Appends enough padding bytes so that the total |
| 111 | /// struct size is a multiple of the field alignment. |
| 112 | void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment); |
| 113 | |
| 114 | /// AppendBytes - Append a given number of bytes to the record. |
| 115 | void AppendBytes(uint64_t NumBytes); |
| 116 | |
| 117 | /// getNextFieldOffsetInBytes - returns where the next field offset is. |
| 118 | uint64_t getNextFieldOffsetInBytes() const; |
| 119 | |
| 120 | unsigned getTypeAlignment(const llvm::Type *Ty) const; |
| 121 | uint64_t getTypeSizeInBytes(const llvm::Type *Ty) const; |
| 122 | |
| 123 | public: |
| 124 | /// ComputeLayout - Return the right record layout for a given record decl. |
| 125 | static CGRecordLayout *ComputeLayout(CodeGenTypes &Types, |
| 126 | const RecordDecl *D); |
| 127 | }; |
| 128 | |
| 129 | } // end namespace CodeGen |
| 130 | } // end namespace clang |
| 131 | |
| 132 | |
| 133 | #endif |