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