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