Add a method to generate a string representation from a TargetData.
[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 "llvm/Support/DataTypes.h"
25 #include <vector>
26 #include <string>
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 BoolAlignment;         // Defaults to 1 byte
38   unsigned char ByteAlignment;         // Defaults to 1 byte
39   unsigned char ShortAlignment;        // Defaults to 2 bytes
40   unsigned char IntAlignment;          // Defaults to 4 bytes
41   unsigned char LongAlignment;         // Defaults to 8 bytes
42   unsigned char FloatAlignment;        // Defaults to 4 bytes
43   unsigned char DoubleAlignment;       // Defaults to 8 bytes
44   unsigned char PointerSize;           // Defaults to 8 bytes
45   unsigned char PointerAlignment;      // Defaults to 8 bytes
46
47 public:
48   TargetData(const std::string &TargetName = "",
49              bool LittleEndian = false,
50              unsigned char PtrSize = 8,
51              unsigned char PtrAl   = 8, unsigned char DoubleAl = 8,
52              unsigned char FloatAl = 4, unsigned char LongAl   = 8,
53              unsigned char IntAl   = 4, unsigned char ShortAl  = 2,
54              unsigned char ByteAl  = 1, unsigned char BoolAl   = 1);
55
56   /// Constructs a TargetData from a string of the following format:
57   /// "E-p:64:64-d:64:64-f:32:32-l:64:64-i:32:32-s:16:16-b:8:8-B:8:8"
58   /// The above string is considered the default, and any values not specified
59   /// in the string will be assumed to be as above.
60   TargetData(const std::string &TargetName,
61              const std::string &TargetDescription);
62   
63   // Copy constructor
64   TargetData (const TargetData &TD) :
65     ImmutablePass(),
66     LittleEndian(TD.isLittleEndian()),
67     BoolAlignment(TD.getBoolAlignment()),
68     ByteAlignment(TD.getByteAlignment()),
69     ShortAlignment(TD.getShortAlignment()),
70     IntAlignment(TD.getIntAlignment()),
71     LongAlignment(TD.getLongAlignment()),
72     FloatAlignment(TD.getFloatAlignment()),
73     DoubleAlignment(TD.getDoubleAlignment()),
74     PointerSize(TD.getPointerSize()),
75     PointerAlignment(TD.getPointerAlignment()) {
76   }
77
78   TargetData(const std::string &ToolName, const Module *M);
79   ~TargetData();  // Not virtual, do not subclass this class
80
81   /// Target endianness...
82   bool          isLittleEndian()       const { return     LittleEndian; }
83   bool          isBigEndian()          const { return    !LittleEndian; }
84
85   /// Target alignment constraints
86   unsigned char getBoolAlignment()     const { return    BoolAlignment; }
87   unsigned char getByteAlignment()     const { return    ByteAlignment; }
88   unsigned char getShortAlignment()    const { return   ShortAlignment; }
89   unsigned char getIntAlignment()      const { return     IntAlignment; }
90   unsigned char getLongAlignment()     const { return    LongAlignment; }
91   unsigned char getFloatAlignment()    const { return   FloatAlignment; }
92   unsigned char getDoubleAlignment()   const { return  DoubleAlignment; }
93   unsigned char getPointerAlignment()  const { return PointerAlignment; }
94   unsigned char getPointerSize()       const { return      PointerSize; }
95   unsigned char getPointerSizeInBits() const { return    8*PointerSize; }
96
97   /// getStringRepresentation - Return the string representation of the
98   /// TargetData.  This representation is in the same format accepted by the
99   /// string constructor above.
100   std::string getStringRepresentation() const;
101
102   /// getTypeSize - Return the number of bytes necessary to hold the specified
103   /// type.
104   ///
105   uint64_t getTypeSize(const Type *Ty) const;
106
107   /// getTypeAlignment - Return the minimum required alignment for the specified
108   /// type.
109   ///
110   unsigned char getTypeAlignment(const Type *Ty) const;
111
112   /// getTypeAlignmentShift - Return the minimum required alignment for the
113   /// specified type, returned as log2 of the value (a shift amount).
114   ///
115   unsigned char getTypeAlignmentShift(const Type *Ty) const;
116
117   /// getIntPtrType - Return an unsigned integer type that is the same size or
118   /// greater to the host pointer size.
119   ///
120   const Type *getIntPtrType() const;
121
122   /// getIndexOffset - return the offset from the beginning of the type for the
123   /// specified indices.  This is used to implement getelementptr.
124   ///
125   uint64_t getIndexedOffset(const Type *Ty,
126                             const std::vector<Value*> &Indices) const;
127
128   /// getStructLayout - Return a StructLayout object, indicating the alignment
129   /// of the struct, its size, and the offsets of its fields.  Note that this
130   /// information is lazily cached.
131   const StructLayout *getStructLayout(const StructType *Ty) const;
132   
133   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
134   /// objects.  If a TargetData object is alive when types are being refined and
135   /// removed, this method must be called whenever a StructType is removed to
136   /// avoid a dangling pointer in this cache.
137   void InvalidateStructLayoutInfo(const StructType *Ty) const;
138 };
139
140 /// StructLayout - used to lazily calculate structure layout information for a
141 /// target machine, based on the TargetData structure.
142 ///
143 class StructLayout {
144 public:
145   std::vector<uint64_t> MemberOffsets;
146   uint64_t StructSize;
147   unsigned StructAlignment;
148
149   /// getElementContainingOffset - Given a valid offset into the structure,
150   /// return the structure index that contains it.
151   ///
152   unsigned getElementContainingOffset(uint64_t Offset) const;
153
154 private:
155   friend class TargetData;   // Only TargetData can create this class
156   StructLayout(const StructType *ST, const TargetData &TD);
157 };
158
159 } // End llvm namespace
160
161 #endif