LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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   TargetData(const std::string &TargetName = "",
00049              bool LittleEndian = false,
00050              unsigned char PtrSize = 8,
00051              unsigned char PtrAl   = 8, unsigned char DoubleAl = 8,
00052              unsigned char FloatAl = 4, unsigned char LongAl   = 8, 
00053              unsigned char IntAl   = 4, unsigned char ShortAl  = 2,
00054              unsigned char ByteAl  = 1, unsigned char BoolAl   = 1);
00055 
00056   // Copy constructor
00057   TargetData (const TargetData &TD) :
00058     ImmutablePass(),
00059     LittleEndian(TD.isLittleEndian()),
00060     BoolAlignment(TD.getBoolAlignment()),
00061     ByteAlignment(TD.getByteAlignment()),
00062     ShortAlignment(TD.getShortAlignment()),
00063     IntAlignment(TD.getIntAlignment()),
00064     LongAlignment(TD.getLongAlignment()),
00065     FloatAlignment(TD.getFloatAlignment()),
00066     DoubleAlignment(TD.getDoubleAlignment()),
00067     PointerSize(TD.getPointerSize()),
00068     PointerAlignment(TD.getPointerAlignment()) {
00069   }
00070     
00071   TargetData(const std::string &ToolName, const Module *M);
00072   ~TargetData();  // Not virtual, do not subclass this class
00073 
00074   /// Target endianness...
00075   bool          isLittleEndian()      const { return     LittleEndian; }
00076   bool          isBigEndian()         const { return    !LittleEndian; }
00077 
00078   /// Target alignment constraints
00079   unsigned char getBoolAlignment()    const { return    BoolAlignment; }
00080   unsigned char getByteAlignment()    const { return    ByteAlignment; }
00081   unsigned char getShortAlignment()   const { return   ShortAlignment; }
00082   unsigned char getIntAlignment()     const { return     IntAlignment; }
00083   unsigned char getLongAlignment()    const { return    LongAlignment; }
00084   unsigned char getFloatAlignment()   const { return   FloatAlignment; }
00085   unsigned char getDoubleAlignment()  const { return  DoubleAlignment; }
00086   unsigned char getPointerAlignment() const { return PointerAlignment; }
00087   unsigned char getPointerSize()      const { return      PointerSize; }
00088 
00089   /// getTypeSize - Return the number of bytes necessary to hold the specified
00090   /// type.
00091   uint64_t getTypeSize(const Type *Ty) const;
00092 
00093   /// getTypeAlignment - Return the minimum required alignment for the specified
00094   /// type.
00095   unsigned char getTypeAlignment(const Type *Ty) const;
00096 
00097   /// getTypeAlignmentShift - Return the minimum required alignment for the
00098   /// specified type, returned as log2 of the value (a shift amount).
00099   unsigned char getTypeAlignmentShift(const Type *Ty) const;
00100 
00101   /// getIntPtrType - Return an unsigned integer type that is the same size or
00102   /// greater to the host pointer size.
00103   const Type *getIntPtrType() const;
00104 
00105   /// getIndexOffset - return the offset from the beginning of the type for the
00106   /// specified indices.  This is used to implement getelementptr.
00107   ///
00108   uint64_t getIndexedOffset(const Type *Ty, 
00109                             const std::vector<Value*> &Indices) const;
00110   
00111   const StructLayout *getStructLayout(const StructType *Ty) const;
00112 };
00113 
00114 // This object is used to lazily calculate structure layout information for a
00115 // target machine, based on the TargetData structure.
00116 //
00117 class StructLayout {
00118 public:
00119   std::vector<uint64_t> MemberOffsets;
00120   uint64_t StructSize;
00121   unsigned StructAlignment;
00122 private:
00123   friend class TargetData;   // Only TargetData can create this class
00124   StructLayout(const StructType *ST, const TargetData &TD);
00125 };
00126 
00127 } // End llvm namespace
00128 
00129 #endif