Committed for compliation. Not yet final.
[oota-llvm.git] / include / llvm / CodeGen / TargetData.h
1 //===-- llvm/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_CODEGEN_TARGETDATA_H
14 #define LLVM_CODEGEN_TARGETDATA_H
15
16 #include "llvm/Type.h"
17 #include "llvm/Annotation.h"
18 #include <vector>
19
20 class StructType;
21 class StructLayout;
22
23 class TargetData {
24   unsigned char ByteAlignment;         // Defaults to 1 bytes
25   unsigned char ShortAlignment;        // Defaults to 2 bytes
26   unsigned char IntAlignment;          // Defaults to 4 bytes
27   unsigned char LongAlignment;         // Defaults to 8 bytes
28   unsigned char FloatAlignment;        // Defaults to 4 bytes
29   unsigned char DoubleAlignment;       // Defaults to 8 bytes
30   unsigned char PointerSize;           // Defaults to 8 bytes
31   unsigned char PointerAlignment;      // Defaults to 8 bytes
32   AnnotationID  AID;                   // AID for structure layout annotation
33  
34   static Annotation *TypeAnFactory(AnnotationID, const Annotable *, void *);
35 public:
36   TargetData(const string &TargetName, unsigned char PtrSize = 8,
37              unsigned char PtrAl = 8, unsigned char DoubleAl = 8,
38              unsigned char FloatAl = 4, unsigned char LongAl = 8, 
39              unsigned char IntAl = 4, unsigned char ShortAl = 2,
40              unsigned char ByteAl = 1);
41   ~TargetData();  // Not virtual, do not subclass this class
42
43   unsigned char getByteAlignment()    const { return    ByteAlignment; }
44   unsigned char getShortAlignment()   const { return   ShortAlignment; }
45   unsigned char getIntAlignment()     const { return     IntAlignment; }
46   unsigned char getLongAlignment()    const { return    LongAlignment; }
47   unsigned char getFloatAlignment()   const { return   FloatAlignment; }
48   unsigned char getDoubleAlignment()  const { return  DoubleAlignment; }
49   unsigned char getPointerAlignment() const { return PointerAlignment; }
50   unsigned char getPointerSize()      const { return PointerSize; }
51   AnnotationID  getStructLayoutAID()  const { return AID; }
52
53   // getTypeSize - Return the number of bytes neccesary to hold the specified
54   // type
55   unsigned      getTypeSize     (const Type *Ty) const;
56
57   // getTypeAlignment - Return the minimum required alignment for the specified
58   // type
59   unsigned char getTypeAlignment(const Type *Ty) const;
60
61   // getIndexOffset - return the offset from the beginning of the type for the
62   // specified indices.  This is used to implement getElementPtr and load and 
63   // stores that include the implicit form of getelementptr.
64   //
65   unsigned      getIndexedOffset(const Type *Ty, 
66                                  const vector<ConstPoolVal*> &Indices) const;
67
68   inline const StructLayout *getStructLayout(const StructType *Ty) const {
69     return (const StructLayout*)((const Type*)Ty)->getOrCreateAnnotation(AID);
70   }
71 };
72
73 // This annotation (attached ONLY to StructType classes) is used to lazily
74 // calculate structure layout information for a target machine, based on the
75 // TargetData structure.
76 //
77 struct StructLayout : public Annotation {
78   vector<unsigned> MemberOffsets;
79   unsigned StructSize;
80   unsigned StructAlignment;
81 private:
82   friend class TargetData;   // Only TargetData can create this class
83   inline StructLayout(const StructType *ST, const TargetData &TD);
84 };
85
86 #endif