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