- Add an endianness field to the TargetData datastructure
[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/Annotation.h"
17 #include "llvm/Pass.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 ByteAlignment;         // Defaults to 1 bytes
28   unsigned char ShortAlignment;        // Defaults to 2 bytes
29   unsigned char IntAlignment;          // Defaults to 4 bytes
30   unsigned char LongAlignment;         // Defaults to 8 bytes
31   unsigned char FloatAlignment;        // Defaults to 4 bytes
32   unsigned char DoubleAlignment;       // Defaults to 8 bytes
33   unsigned char PointerSize;           // Defaults to 8 bytes
34   unsigned char IntegerRegSize;        // Defaults to PointerSize = 8 bytes
35   unsigned char PointerAlignment;      // Defaults to 8 bytes
36   AnnotationID  AID;                   // AID for structure layout annotation
37  
38   static Annotation *TypeAnFactory(AnnotationID, const Annotable *, void *);
39 public:
40   TargetData(const std::string &TargetName = "SparcV9",
41              bool LittleEndian = false,
42              unsigned char IntRegSize = 8,
43              unsigned char PtrSize = 8,
44              unsigned char PtrAl = 8, unsigned char DoubleAl = 8,
45              unsigned char FloatAl = 4, unsigned char LongAl = 8, 
46              unsigned char IntAl = 4, unsigned char ShortAl = 2,
47              unsigned char ByteAl = 1);
48   ~TargetData();  // Not virtual, do not subclass this class
49
50   /// Target endianness...
51   bool          isLittleEndian()      const { return     LittleEndian; }
52   bool          isBigEndian()         const { return    !LittleEndian; }
53
54   /// Target alignment constraints
55   unsigned char getByteAlignment()    const { return    ByteAlignment; }
56   unsigned char getShortAlignment()   const { return   ShortAlignment; }
57   unsigned char getIntAlignment()     const { return     IntAlignment; }
58   unsigned char getLongAlignment()    const { return    LongAlignment; }
59   unsigned char getFloatAlignment()   const { return   FloatAlignment; }
60   unsigned char getDoubleAlignment()  const { return  DoubleAlignment; }
61   unsigned char getPointerAlignment() const { return PointerAlignment; }
62   unsigned char getPointerSize()      const { return      PointerSize; }
63   unsigned char getIntegerRegize()    const { return   IntegerRegSize; }
64   AnnotationID  getStructLayoutAID()  const { return AID; }
65
66   // getTypeSize - Return the number of bytes neccesary to hold the specified
67   // type
68   uint64_t      getTypeSize     (const Type *Ty) const;
69
70   // getTypeAlignment - Return the minimum required alignment for the specified
71   // type
72   unsigned char getTypeAlignment(const Type *Ty) const;
73
74   // getIndexOffset - return the offset from the beginning of the type for the
75   // specified indices.  This is used to implement getelementptr.
76   //
77   uint64_t      getIndexedOffset(const Type *Ty, 
78                                  const std::vector<Value*> &Indices) const;
79   
80   inline const StructLayout *getStructLayout(const StructType *Ty) const {
81     return (const StructLayout*)
82          ((const Annotable*)Ty)->getOrCreateAnnotation(AID);
83   }
84 };
85
86 // This annotation (attached ONLY to StructType classes) is used to lazily
87 // calculate structure layout information for a target machine, based on the
88 // TargetData structure.
89 //
90 struct StructLayout : public Annotation {
91   std::vector<uint64_t> MemberOffsets;
92   uint64_t StructSize;
93   unsigned StructAlignment;
94 private:
95   friend class TargetData;   // Only TargetData can create this class
96   inline StructLayout(const StructType *ST, const TargetData &TD);
97 };
98
99 #endif