Default ctor doesn't provide name
[oota-llvm.git] / include / llvm / Target / TargetData.h
1 //===-- llvm/Target/TargetData.h - Data size & alignment routines-*- C++ -*-==//
2 //
3 // This file defines target properties related to datatype size/offset/alignment
4 // information.  It uses lazy annotations to cache information about how 
5 // structure types are laid out and used.
6 //
7 // This structure should be created once, filled in if the defaults are not
8 // correct and then passed around by const&.  None of the members functions
9 // require modification to the object.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TARGET_TARGETDATA_H
14 #define LLVM_TARGET_TARGETDATA_H
15
16 #include "llvm/Pass.h"
17 #include "Support/Annotation.h"
18 #include "Support/DataTypes.h"
19 #include <vector>
20 class Value;
21 class Type;
22 class StructType;
23 class StructLayout;
24
25 class TargetData : public ImmutablePass {
26   bool          LittleEndian;          // Defaults to false
27   unsigned char SubWordDataSize;       // Defaults to 1 byte (no rounding up)
28   unsigned char ByteAlignment;         // Defaults to 1 bytes
29   unsigned char ShortAlignment;        // Defaults to 2 bytes
30   unsigned char IntAlignment;          // Defaults to 4 bytes
31   unsigned char LongAlignment;         // Defaults to 8 bytes
32   unsigned char FloatAlignment;        // Defaults to 4 bytes
33   unsigned char DoubleAlignment;       // Defaults to 8 bytes
34   unsigned char PointerSize;           // Defaults to 8 bytes
35   unsigned char IntegerRegSize;        // Defaults to PointerSize = 8 bytes
36   unsigned char PointerAlignment;      // Defaults to 8 bytes
37   AnnotationID  AID;                   // AID for structure layout annotation
38  
39   static Annotation *TypeAnFactory(AnnotationID, const Annotable *, void *);
40 public:
41   TargetData(const std::string &TargetName = "",
42              bool LittleEndian = false,
43              unsigned char SubWordDataSize = 1,
44              unsigned char IntRegSize = 8,
45              unsigned char PtrSize = 8,
46              unsigned char PtrAl = 8, unsigned char DoubleAl = 8,
47              unsigned char FloatAl = 4, unsigned char LongAl = 8, 
48              unsigned char IntAl = 4, unsigned char ShortAl = 2,
49              unsigned char ByteAl = 1);
50   TargetData(const std::string &ToolName, const Module *M);
51   ~TargetData();  // Not virtual, do not subclass this class
52
53   /// Target endianness...
54   bool          isLittleEndian()      const { return     LittleEndian; }
55   bool          isBigEndian()         const { return    !LittleEndian; }
56
57   /// Target alignment constraints
58   unsigned char getSubWordDataSize()  const { return  SubWordDataSize; }
59   unsigned char getByteAlignment()    const { return    ByteAlignment; }
60   unsigned char getShortAlignment()   const { return   ShortAlignment; }
61   unsigned char getIntAlignment()     const { return     IntAlignment; }
62   unsigned char getLongAlignment()    const { return    LongAlignment; }
63   unsigned char getFloatAlignment()   const { return   FloatAlignment; }
64   unsigned char getDoubleAlignment()  const { return  DoubleAlignment; }
65   unsigned char getPointerAlignment() const { return PointerAlignment; }
66   unsigned char getPointerSize()      const { return      PointerSize; }
67   unsigned char getIntegerRegize()    const { return   IntegerRegSize; }
68   AnnotationID  getStructLayoutAID()  const { return AID; }
69
70   // getTypeSize - Return the number of bytes neccesary to hold the specified
71   // type
72   uint64_t      getTypeSize     (const Type *Ty) const;
73
74   // getTypeAlignment - Return the minimum required alignment for the specified
75   // type
76   unsigned char getTypeAlignment(const Type *Ty) const;
77
78   // getIndexOffset - return the offset from the beginning of the type for the
79   // specified indices.  This is used to implement getelementptr.
80   //
81   uint64_t      getIndexedOffset(const Type *Ty, 
82                                  const std::vector<Value*> &Indices) const;
83   
84   inline const StructLayout *getStructLayout(const StructType *Ty) const {
85     return (const StructLayout*)
86          ((const Annotable*)Ty)->getOrCreateAnnotation(AID);
87   }
88 };
89
90 // This annotation (attached ONLY to StructType classes) is used to lazily
91 // calculate structure layout information for a target machine, based on the
92 // TargetData structure.
93 //
94 struct StructLayout : public Annotation {
95   std::vector<uint64_t> MemberOffsets;
96   uint64_t StructSize;
97   unsigned StructAlignment;
98 private:
99   friend class TargetData;   // Only TargetData can create this class
100   inline StructLayout(const StructType *ST, const TargetData &TD);
101 };
102
103 #endif