LLVM API Documentation

TargetData.h

Go to the documentation of this file.
00001 //===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines target properties related to datatype size/offset/alignment
00011 // information.  It uses lazy annotations to cache information about how
00012 // structure types are laid out and used.
00013 //
00014 // This structure should be created once, filled in if the defaults are not
00015 // correct and then passed around by const&.  None of the members functions
00016 // require modification to the object.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_TARGET_TARGETDATA_H
00021 #define LLVM_TARGET_TARGETDATA_H
00022 
00023 #include "llvm/Pass.h"
00024 #include "llvm/Support/DataTypes.h"
00025 #include <vector>
00026 #include <string>
00027 
00028 namespace llvm {
00029 
00030 class Value;
00031 class Type;
00032 class StructType;
00033 class StructLayout;
00034 
00035 class TargetData : public ImmutablePass {
00036   bool          LittleEndian;          // Defaults to false
00037   unsigned char BoolAlignment;         // Defaults to 1 byte
00038   unsigned char ByteAlignment;         // Defaults to 1 byte
00039   unsigned char ShortAlignment;        // Defaults to 2 bytes
00040   unsigned char IntAlignment;          // Defaults to 4 bytes
00041   unsigned char LongAlignment;         // Defaults to 8 bytes
00042   unsigned char FloatAlignment;        // Defaults to 4 bytes
00043   unsigned char DoubleAlignment;       // Defaults to 8 bytes
00044   unsigned char PointerSize;           // Defaults to 8 bytes
00045   unsigned char PointerAlignment;      // Defaults to 8 bytes
00046 
00047 public:
00048   /// Default ctor - This has to exist, because this is a pass, but it should
00049   /// never be used.
00050   TargetData() {
00051     assert(0 && "ERROR: Bad TargetData ctor used.  "
00052            "Tool did not specify a TargetData to use?");
00053     abort();
00054   }
00055     
00056   /// Constructs a TargetData from a string of the following format:
00057   /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8"
00058   /// The above string is considered the default, and any values not specified
00059   /// in the string will be assumed to be as above.
00060   TargetData(const std::string &TargetDescription) {
00061     init(TargetDescription);
00062   }
00063 
00064   /// Initialize target data from properties stored in the module.
00065   TargetData(const Module *M);
00066 
00067   TargetData(const TargetData &TD) : 
00068     ImmutablePass(),
00069     LittleEndian(TD.isLittleEndian()),
00070     BoolAlignment(TD.getBoolAlignment()),
00071     ByteAlignment(TD.getByteAlignment()),
00072     ShortAlignment(TD.getShortAlignment()),
00073     IntAlignment(TD.getIntAlignment()),
00074     LongAlignment(TD.getLongAlignment()),
00075     FloatAlignment(TD.getFloatAlignment()),
00076     DoubleAlignment(TD.getDoubleAlignment()),
00077     PointerSize(TD.getPointerSize()),
00078     PointerAlignment(TD.getPointerAlignment()) {
00079   }
00080 
00081   ~TargetData();  // Not virtual, do not subclass this class
00082 
00083   /// init - Specify configuration if not available at ctor time.
00084   ///
00085   void init(const std::string &TargetDescription);
00086   
00087   
00088   /// Target endianness...
00089   bool          isLittleEndian()       const { return     LittleEndian; }
00090   bool          isBigEndian()          const { return    !LittleEndian; }
00091 
00092   /// Target alignment constraints
00093   unsigned char getBoolAlignment()     const { return    BoolAlignment; }
00094   unsigned char getByteAlignment()     const { return    ByteAlignment; }
00095   unsigned char getShortAlignment()    const { return   ShortAlignment; }
00096   unsigned char getIntAlignment()      const { return     IntAlignment; }
00097   unsigned char getLongAlignment()     const { return    LongAlignment; }
00098   unsigned char getFloatAlignment()    const { return   FloatAlignment; }
00099   unsigned char getDoubleAlignment()   const { return  DoubleAlignment; }
00100   unsigned char getPointerAlignment()  const { return PointerAlignment; }
00101   unsigned char getPointerSize()       const { return      PointerSize; }
00102   unsigned char getPointerSizeInBits() const { return    8*PointerSize; }
00103 
00104   /// getStringRepresentation - Return the string representation of the
00105   /// TargetData.  This representation is in the same format accepted by the
00106   /// string constructor above.
00107   std::string getStringRepresentation() const;
00108 
00109   /// getTypeSize - Return the number of bytes necessary to hold the specified
00110   /// type.
00111   ///
00112   uint64_t getTypeSize(const Type *Ty) const;
00113 
00114   /// getTypeAlignment - Return the minimum required alignment for the specified
00115   /// type.
00116   ///
00117   unsigned char getTypeAlignment(const Type *Ty) const;
00118 
00119   /// getTypeAlignmentShift - Return the minimum required alignment for the
00120   /// specified type, returned as log2 of the value (a shift amount).
00121   ///
00122   unsigned char getTypeAlignmentShift(const Type *Ty) const;
00123 
00124   /// getIntPtrType - Return an unsigned integer type that is the same size or
00125   /// greater to the host pointer size.
00126   ///
00127   const Type *getIntPtrType() const;
00128 
00129   /// getIndexOffset - return the offset from the beginning of the type for the
00130   /// specified indices.  This is used to implement getelementptr.
00131   ///
00132   uint64_t getIndexedOffset(const Type *Ty,
00133                             const std::vector<Value*> &Indices) const;
00134 
00135   /// getStructLayout - Return a StructLayout object, indicating the alignment
00136   /// of the struct, its size, and the offsets of its fields.  Note that this
00137   /// information is lazily cached.
00138   const StructLayout *getStructLayout(const StructType *Ty) const;
00139   
00140   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
00141   /// objects.  If a TargetData object is alive when types are being refined and
00142   /// removed, this method must be called whenever a StructType is removed to
00143   /// avoid a dangling pointer in this cache.
00144   void InvalidateStructLayoutInfo(const StructType *Ty) const;
00145 };
00146 
00147 /// StructLayout - used to lazily calculate structure layout information for a
00148 /// target machine, based on the TargetData structure.
00149 ///
00150 class StructLayout {
00151 public:
00152   std::vector<uint64_t> MemberOffsets;
00153   uint64_t StructSize;
00154   unsigned StructAlignment;
00155 
00156   /// getElementContainingOffset - Given a valid offset into the structure,
00157   /// return the structure index that contains it.
00158   ///
00159   unsigned getElementContainingOffset(uint64_t Offset) const;
00160 
00161 private:
00162   friend class TargetData;   // Only TargetData can create this class
00163   StructLayout(const StructType *ST, const TargetData &TD);
00164 };
00165 
00166 } // End llvm namespace
00167 
00168 #endif