make doxygen comment much better. Patch by B. Scott Michel!
[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 class GlobalVariable;
35
36 class TargetData : public ImmutablePass {
37   bool          LittleEndian;          // Defaults to false
38   unsigned char BoolAlignment;         // Defaults to 1 byte
39   unsigned char ByteAlignment;         // Defaults to 1 byte
40   unsigned char ShortAlignment;        // Defaults to 2 bytes
41   unsigned char IntAlignment;          // Defaults to 4 bytes
42   unsigned char LongAlignment;         // Defaults to 8 bytes
43   unsigned char FloatAlignment;        // Defaults to 4 bytes
44   unsigned char DoubleAlignment;       // Defaults to 8 bytes
45   unsigned char PointerSize;           // Defaults to 8 bytes
46   unsigned char PointerAlignment;      // Defaults to 8 bytes
47
48 public:
49   /// Default ctor - This has to exist, because this is a pass, but it should
50   /// never be used.
51   TargetData() {
52     assert(0 && "ERROR: Bad TargetData ctor used.  "
53            "Tool did not specify a TargetData to use?");
54     abort();
55   }
56     
57   /// Constructs a TargetData from a string of the following format:
58   /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8"
59   /// The above string is considered the default, and any values not specified
60   /// in the string will be assumed to be as above.
61   TargetData(const std::string &TargetDescription) {
62     init(TargetDescription);
63   }
64
65   /// Initialize target data from properties stored in the module.
66   TargetData(const Module *M);
67
68   TargetData(const TargetData &TD) : 
69     ImmutablePass(),
70     LittleEndian(TD.isLittleEndian()),
71     BoolAlignment(TD.getBoolAlignment()),
72     ByteAlignment(TD.getByteAlignment()),
73     ShortAlignment(TD.getShortAlignment()),
74     IntAlignment(TD.getIntAlignment()),
75     LongAlignment(TD.getLongAlignment()),
76     FloatAlignment(TD.getFloatAlignment()),
77     DoubleAlignment(TD.getDoubleAlignment()),
78     PointerSize(TD.getPointerSize()),
79     PointerAlignment(TD.getPointerAlignment()) {
80   }
81
82   ~TargetData();  // Not virtual, do not subclass this class
83
84   /// Parse a target data layout string and initialize TargetData members.
85   ///
86   /// Parse a target data layout string, initializing the various TargetData
87   /// members along the way. A TargetData specification string looks like
88   /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8" and specifies the
89   /// target's endianess, the alignments of various data types and
90   /// the size of pointers. The "-" is used as a separator and ":"
91   /// separates a token from its argument. Alignment is indicated in bits
92   /// and internally converted to the appropriate number of bytes.
93   ///
94   /// Valid tokens:
95   /// <br>
96   /// <em>E</em> specifies big endian architecture (1234) [default]<br>
97   /// <em>e</em> specifies little endian architecture (4321) <br>
98   /// <em>p:[ptr size]:[ptr align]</em> specifies pointer size and alignment
99   /// [default = 64:64] <br>
100   /// <em>d:[align]</em> specifies double floating point alignment
101   /// [default = 64] <br>
102   /// <em>f:[align]</em> specifies single floating point alignment
103   /// [default = 32] <br>
104   /// <em>l:[align]</em> specifies long integer alignment
105   /// [default = 64] <br>
106   /// <em>i:[align]</em> specifies integer alignment
107   /// [default = 32] <br>
108   /// <em>s:[align]</em> specifies short integer alignment
109   /// [default = 16] <br>
110   /// <em>b:[align]</em> specifies byte data type alignment
111   /// [default = 8] <br>
112   /// <em>B:[align]</em> specifies boolean data type alignment
113   /// [default = 8] <br>
114   ///
115   /// All other token types are silently ignored.
116   void init(const std::string &TargetDescription);
117   
118   
119   /// Target endianness...
120   bool          isLittleEndian()       const { return     LittleEndian; }
121   bool          isBigEndian()          const { return    !LittleEndian; }
122
123   /// Target alignment constraints
124   unsigned char getBoolAlignment()     const { return    BoolAlignment; }
125   unsigned char getByteAlignment()     const { return    ByteAlignment; }
126   unsigned char getShortAlignment()    const { return   ShortAlignment; }
127   unsigned char getIntAlignment()      const { return     IntAlignment; }
128   unsigned char getLongAlignment()     const { return    LongAlignment; }
129   unsigned char getFloatAlignment()    const { return   FloatAlignment; }
130   unsigned char getDoubleAlignment()   const { return  DoubleAlignment; }
131   unsigned char getPointerAlignment()  const { return PointerAlignment; }
132   unsigned char getPointerSize()       const { return      PointerSize; }
133   unsigned char getPointerSizeInBits() const { return    8*PointerSize; }
134
135   /// getStringRepresentation - Return the string representation of the
136   /// TargetData.  This representation is in the same format accepted by the
137   /// string constructor above.
138   std::string getStringRepresentation() const;
139
140   /// getTypeSize - Return the number of bytes necessary to hold the specified
141   /// type.
142   ///
143   uint64_t getTypeSize(const Type *Ty) const;
144
145   /// getTypeAlignment - Return the minimum required alignment for the specified
146   /// type.
147   ///
148   unsigned char getTypeAlignment(const Type *Ty) const;
149
150   /// getTypeAlignmentShift - Return the minimum required alignment for the
151   /// specified type, returned as log2 of the value (a shift amount).
152   ///
153   unsigned char getTypeAlignmentShift(const Type *Ty) const;
154
155   /// getIntPtrType - Return an unsigned integer type that is the same size or
156   /// greater to the host pointer size.
157   ///
158   const Type *getIntPtrType() const;
159
160   /// getIndexOffset - return the offset from the beginning of the type for the
161   /// specified indices.  This is used to implement getelementptr.
162   ///
163   uint64_t getIndexedOffset(const Type *Ty,
164                             const std::vector<Value*> &Indices) const;
165
166   /// getStructLayout - Return a StructLayout object, indicating the alignment
167   /// of the struct, its size, and the offsets of its fields.  Note that this
168   /// information is lazily cached.
169   const StructLayout *getStructLayout(const StructType *Ty) const;
170   
171   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
172   /// objects.  If a TargetData object is alive when types are being refined and
173   /// removed, this method must be called whenever a StructType is removed to
174   /// avoid a dangling pointer in this cache.
175   void InvalidateStructLayoutInfo(const StructType *Ty) const;
176
177   /// getPreferredAlignmentLog - Return the preferred alignment of the
178   /// specified global, returned in log form.  This includes an explicitly
179   /// requested alignment (if the global has one).
180   unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
181 };
182
183 /// StructLayout - used to lazily calculate structure layout information for a
184 /// target machine, based on the TargetData structure.
185 ///
186 class StructLayout {
187 public:
188   std::vector<uint64_t> MemberOffsets;
189   uint64_t StructSize;
190   unsigned StructAlignment;
191
192   /// getElementContainingOffset - Given a valid offset into the structure,
193   /// return the structure index that contains it.
194   ///
195   unsigned getElementContainingOffset(uint64_t Offset) const;
196
197 private:
198   friend class TargetData;   // Only TargetData can create this class
199   StructLayout(const StructType *ST, const TargetData &TD);
200 };
201
202 } // End llvm namespace
203
204 #endif