eliminate some extraneous methods in SDNode
[oota-llvm.git] / include / llvm / CodeGen / MachOWriter.h
index 6f0d9575a69f69495fff4c6975e865cc59b9c58c..bff94b4b2f4848d6c5772c2f5f0e000e01941506 100644 (file)
 #ifndef LLVM_CODEGEN_MACHOWRITER_H
 #define LLVM_CODEGEN_MACHOWRITER_H
 
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineRelocation.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
-#include <list>
+#include "llvm/Target/TargetMachOWriterInfo.h"
 
 namespace llvm {
   class GlobalVariable;
   class Mangler;
   class MachineCodeEmitter;
   class MachOCodeEmitter;
-
+  class OutputBuffer;
+
+  /// MachOSym - This struct contains information about each symbol that is
+  /// added to logical symbol table for the module.  This is eventually
+  /// turned into a real symbol table in the file.
+  struct MachOSym {
+    const GlobalValue *GV;    // The global value this corresponds to.
+    std::string GVName;       // The mangled name of the global value.
+    uint32_t    n_strx;       // index into the string table
+    uint8_t     n_type;       // type flag
+    uint8_t     n_sect;       // section number or NO_SECT
+    int16_t     n_desc;       // see <mach-o/stab.h>
+    uint64_t    n_value;      // value for this symbol (or stab offset)
+    
+    // Constants for the n_sect field
+    // see <mach-o/nlist.h>
+    enum { NO_SECT = 0 };   // symbol is not in any section
+
+    // Constants for the n_type field
+    // see <mach-o/nlist.h>
+    enum { N_UNDF  = 0x0,  // undefined, n_sect == NO_SECT
+           N_ABS   = 0x2,  // absolute, n_sect == NO_SECT
+           N_SECT  = 0xe,  // defined in section number n_sect
+           N_PBUD  = 0xc,  // prebound undefined (defined in a dylib)
+           N_INDR  = 0xa   // indirect
+    };
+    // The following bits are OR'd into the types above. For example, a type
+    // of 0x0f would be an external N_SECT symbol (0x0e | 0x01).
+    enum { N_EXT  = 0x01,   // external symbol bit
+           N_PEXT = 0x10    // private external symbol bit
+    };
+    
+    // Constants for the n_desc field
+    // see <mach-o/loader.h>
+    enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY          = 0,
+           REFERENCE_FLAG_UNDEFINED_LAZY              = 1,
+           REFERENCE_FLAG_DEFINED                     = 2,
+           REFERENCE_FLAG_PRIVATE_DEFINED             = 3,
+           REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY  = 4,
+           REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY      = 5
+    };
+    enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped
+           N_WEAK_REF      = 0x0040, // symbol is weak referenced
+           N_WEAK_DEF      = 0x0080  // coalesced symbol is a weak definition
+    };
+    
+    MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
+             TargetMachine &TM);
+  };
+      
   /// MachOWriter - This class implements the common target-independent code for
   /// writing Mach-O files.  Targets should derive a class from this to
   /// parameterize the output format.
@@ -36,11 +87,13 @@ namespace llvm {
     MachineCodeEmitter &getMachineCodeEmitter() const {
       return *(MachineCodeEmitter*)MCE;
     }
+    virtual ~MachOWriter();
 
-    ~MachOWriter();
+    virtual const char *getPassName() const {
+      return "Mach-O Writer";
+    }
 
     typedef std::vector<unsigned char> DataBuffer;
-
   protected:
     MachOWriter(std::ostream &O, TargetMachine &TM);
 
@@ -55,7 +108,7 @@ namespace llvm {
     /// Mang - The object used to perform name mangling for this module.
     ///
     Mangler *Mang;
-
+    
     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
     /// code for functions to the .o file.
     MachOCodeEmitter *MCE;
@@ -78,8 +131,6 @@ namespace llvm {
     /// specific architecture type/subtype pair that is emitted to the file.
     struct MachOHeader {
       uint32_t  magic;      // mach magic number identifier
-      uint32_t  cputype;    // cpu specifier
-      uint32_t  cpusubtype; // machine specifier
       uint32_t  filetype;   // type of file
       uint32_t  ncmds;      // number of load commands
       uint32_t  sizeofcmds; // the size of all the load commands
@@ -90,25 +141,6 @@ namespace llvm {
       /// up for emission to the file.
       DataBuffer HeaderData;
 
-      // Constants for the cputype field
-      // see <mach/machine.h>
-      enum { CPU_TYPE_I386      = 7,
-             CPU_TYPE_X86_64    = 7 | 0x1000000,
-             CPU_TYPE_ARM       = 12,
-             CPU_TYPE_SPARC     = 14,
-             CPU_TYPE_POWERPC   = 18,
-             CPU_TYPE_POWERPC64 = 18 | 0x1000000
-      };
-      
-      // Constants for the cpusubtype field
-      // see <mach/machine.h>
-      enum { CPU_SUBTYPE_I386_ALL    = 3,
-             CPU_SUBTYPE_X86_64_ALL  = 3,
-             CPU_SUBTYPE_ARM_ALL     = 0,
-             CPU_SUBTYPE_SPARC_ALL   = 0,
-             CPU_SUBTYPE_POWERPC_ALL = 0
-      };
-             
       // Constants for the filetype field
       // see <mach-o/loader.h> for additional info on the various types
       enum { MH_OBJECT     = 1, // relocatable object file
@@ -176,8 +208,8 @@ namespace llvm {
                 // stack execution privilege.  Only used in MH_EXECUTE filetype
       };
 
-      MachOHeader() : magic(0), cputype(0), cpusubtype(0), filetype(0),
-                      ncmds(0), sizeofcmds(0), flags(0), reserved(0) { }
+      MachOHeader() : magic(0), filetype(0), ncmds(0), sizeofcmds(0), flags(0),
+                      reserved(0) { }
       
       /// cmdSize - This routine returns the size of the MachOSection as written
       /// to disk, depending on whether the destination is a 64 bit Mach-O file.
@@ -204,8 +236,6 @@ namespace llvm {
     /// the file, and then emit during finalization.
     MachOHeader Header;
     
-  private:
-
     /// MachOSegment - This struct contains the necessary information to
     /// emit the load commands for each section in the file.
     struct MachOSegment {
@@ -221,13 +251,31 @@ namespace llvm {
       uint32_t    nsects;   // number of sections in this segment
       uint32_t    flags;    // flags
       
+      // The following constants are getting pulled in by one of the
+      // system headers, which creates a neat clash with the enum.
+#if !defined(VM_PROT_NONE)
+#define VM_PROT_NONE           0x00
+#endif
+#if !defined(VM_PROT_READ)
+#define VM_PROT_READ           0x01
+#endif
+#if !defined(VM_PROT_WRITE)
+#define VM_PROT_WRITE          0x02
+#endif
+#if !defined(VM_PROT_EXECUTE)
+#define VM_PROT_EXECUTE                0x04
+#endif
+#if !defined(VM_PROT_ALL)
+#define VM_PROT_ALL            0x07
+#endif
+
       // Constants for the vm protection fields
       // see <mach-o/vm_prot.h>
-      enum { VM_PROT_NONE    = 0x00
-             VM_PROT_READ    = 0x01, // read permission
-             VM_PROT_WRITE   = 0x02, // write permission
-             VM_PROT_EXECUTE = 0x04, // execute permission,
-             VM_PROT_ALL     = 0x07
+      enum { SEG_VM_PROT_NONE     = VM_PROT_NONE
+             SEG_VM_PROT_READ     = VM_PROT_READ, // read permission
+             SEG_VM_PROT_WRITE    = VM_PROT_WRITE, // write permission
+             SEG_VM_PROT_EXECUTE  = VM_PROT_EXECUTE,
+             SEG_VM_PROT_ALL      = VM_PROT_ALL
       };
       
       // Constants for the cmd field
@@ -276,6 +324,15 @@ namespace llvm {
       /// SectionData - The actual data for this section which we are building
       /// up for emission to the file.
       DataBuffer SectionData;
+
+      /// RelocBuffer - A buffer to hold the mach-o relocations before we write
+      /// them out at the appropriate location in the file.
+      DataBuffer RelocBuffer;
+      
+      /// Relocations - The relocations that we have encountered so far in this 
+      /// section that we will need to convert to MachORelocation entries when
+      /// the file is written.
+      std::vector<MachineRelocation> Relocations;
       
       // Constants for the section types (low 8 bits of flags field)
       // see <mach-o/loader.h>
@@ -348,46 +405,66 @@ namespace llvm {
       }
 
       MachOSection(const std::string &seg, const std::string &sect)
-        : sectname(sect), segname(seg), addr(0), size(0), offset(0), align(0),
+        : sectname(sect), segname(seg), addr(0), size(0), offset(0), align(2),
           reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0),
           reserved3(0) { }
     };
 
+  private:
+
     /// SectionList - This is the list of sections that we have emitted to the
     /// file.  Once the file has been completely built, the segment load command
     /// SectionCommands are constructed from this info.
-    std::list<MachOSection> SectionList;
+    std::vector<MachOSection*> SectionList;
 
     /// SectionLookup - This is a mapping from section name to SectionList entry
     std::map<std::string, MachOSection*> SectionLookup;
+    
+    /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
+    /// to aid in emitting relocations.
+    std::map<GlobalValue*, MachOSection*> GVSection;
+
+    /// GVOffset - This is a mapping from a GlobalValue to an offset from the 
+    /// start of the section in which the GV resides, to aid in emitting
+    /// relocations.
+    std::map<GlobalValue*, intptr_t> GVOffset;
 
     /// getSection - Return the section with the specified name, creating a new
     /// section if one does not already exist.
-    MachOSection &getSection(const std::string &seg, const std::string &sect,
+    MachOSection *getSection(const std::string &seg, const std::string &sect,
                              unsigned Flags = 0) {
-      MachOSection *&SN = SectionLookup[seg+sect];
-      if (SN) return *SN;
-
-      SectionList.push_back(MachOSection(seg, sect));
-      SN = &SectionList.back();
-      SN->Index = SectionList.size();
-      SN->flags = MachOSection::S_REGULAR | Flags;
-      return *SN;
+      MachOSection *MOS = SectionLookup[seg+sect];
+      if (MOS) return MOS;
+
+      MOS = new MachOSection(seg, sect);
+      SectionList.push_back(MOS);
+      MOS->Index = SectionList.size();
+      MOS->flags = MachOSection::S_REGULAR | Flags;
+      SectionLookup[seg+sect] = MOS;
+      return MOS;
     }
-    MachOSection &getTextSection() {
-      return getSection("__TEXT", "__text", 
-                        MachOSection::S_ATTR_PURE_INSTRUCTIONS |
-                        MachOSection::S_ATTR_SOME_INSTRUCTIONS);
+    MachOSection *getTextSection(bool isCode = true) {
+      if (isCode)
+        return getSection("__TEXT", "__text", 
+                          MachOSection::S_ATTR_PURE_INSTRUCTIONS |
+                          MachOSection::S_ATTR_SOME_INSTRUCTIONS);
+      else
+        return getSection("__TEXT", "__text");
     }
-    MachOSection &getBSSSection() {
+    MachOSection *getBSSSection() {
       return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
     }
-    MachOSection &getDataSection() {
+    MachOSection *getDataSection() {
       return getSection("__DATA", "__data");
     }
-    MachOSection &getConstSection(const Type *Ty) {
-      // FIXME: support cstring literals and pointer literal
-      if (Ty->isPrimitiveType()) {
+    MachOSection *getConstSection(Constant *C) {
+      const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
+      if (CVA && CVA->isCString())
+        return getSection("__TEXT", "__cstring", 
+                          MachOSection::S_CSTRING_LITERALS);
+      
+      const Type *Ty = C->getType();
+      if (Ty->isPrimitiveType() || Ty->isInteger()) {
         unsigned Size = TM.getTargetData()->getTypeSize(Ty);
         switch(Size) {
         default: break; // Fall through to __TEXT,__const
@@ -404,6 +481,12 @@ namespace llvm {
       }
       return getSection("__TEXT", "__const");
     }
+    MachOSection *getJumpTableSection() {
+      if (TM.getRelocationModel() == Reloc::PIC_)
+        return getTextSection(false);
+      else
+        return getSection("__TEXT", "__const");
+    }
     
     /// MachOSymTab - This struct contains information about the offsets and 
     /// size of symbol table information.
@@ -467,63 +550,36 @@ namespace llvm {
     /// DySymTab - symbol table info for the dynamic link editor
     MachODySymTab DySymTab;
 
-    /// MachOSym - This struct contains information about each symbol that is
-    /// added to logical symbol table for the module.  This is eventually
-    /// turned into a real symbol table in the file.
-    struct MachOSym {
-      const GlobalValue *GV;    // The global value this corresponds to.
-      uint32_t  n_strx;         // index into the string table
-      uint8_t   n_type;         // type flag
-      uint8_t   n_sect;         // section number or NO_SECT
-      int16_t   n_desc;         // see <mach-o/stab.h>
-      uint64_t  n_value;        // value for this symbol (or stab offset)
-      
-      // Constants for the n_sect field
-      // see <mach-o/nlist.h>
-      enum { NO_SECT = 0 };   // symbol is not in any section
-
-      // Constants for the n_type field
-      // see <mach-o/nlist.h>
-      enum { N_UNDF  = 0x0,  // undefined, n_sect == NO_SECT
-             N_ABS   = 0x2,  // absolute, n_sect == NO_SECT
-             N_SECT  = 0xe,  // defined in section number n_sect
-             N_PBUD  = 0xc,  // prebound undefined (defined in a dylib)
-             N_INDR  = 0xa   // indirect
-      };
-      // The following bits are OR'd into the types above. For example, a type
-      // of 0x0f would be an external N_SECT symbol (0x0e | 0x01).
-      enum { N_EXT  = 0x01,   // external symbol bit
-             N_PEXT = 0x10    // private external symbol bit
-      };
-      
-      // Constants for the n_desc field
-      // see <mach-o/loader.h>
-      enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY          = 0,
-             REFERENCE_FLAG_UNDEFINED_LAZY              = 1,
-             REFERENCE_FLAG_DEFINED                     = 2,
-             REFERENCE_FLAG_PRIVATE_DEFINED             = 3,
-             REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY  = 4,
-             REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY      = 5
-      };
-      enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped
-             N_WEAK_REF      = 0x0040, // symbol is weak referenced
-             N_WEAK_DEF      = 0x0080  // coalesced symbol is a weak definition
-      };
-      
-      /// entrySize - This routine returns the size of a symbol table entry as
-      /// written to disk.
-      static unsigned entrySize() { return 12; }
-
-      MachOSym(const GlobalValue *gv, uint8_t sect) : GV(gv), n_strx(0), 
-        n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect), n_desc(0),
-        n_value(0) {}
+    struct MachOSymCmp {
+      // FIXME: this does not appear to be sorting 'f' after 'F'
+      bool operator()(const MachOSym &LHS, const MachOSym &RHS) {
+        return LHS.GVName < RHS.GVName;
+      }
     };
 
+    /// PartitionByLocal - Simple boolean predicate that returns true if Sym is
+    /// a local symbol rather than an external symbol.
+    static bool PartitionByLocal(const MachOSym &Sym);
+
+    /// PartitionByDefined - Simple boolean predicate that returns true if Sym 
+    /// is defined in this module.
+    static bool PartitionByDefined(const MachOSym &Sym);
+
+  protected:
+  
     /// SymbolTable - This is the list of symbols we have emitted to the file.
     /// This actually gets rearranged before emission to the file (to put the
     /// local symbols first in the list).
     std::vector<MachOSym> SymbolTable;
     
+    /// SymT - A buffer to hold the symbol table before we write it out at the
+    /// appropriate location in the file.
+    DataBuffer SymT;
+    
+    /// StrT - A buffer to hold the string table before we write it out at the
+    /// appropriate location in the file.
+    DataBuffer StrT;
+    
     /// PendingSyms - This is a list of externally defined symbols that we have
     /// been asked to emit, but have not seen a reference to.  When a reference
     /// is seen, the symbol will move from this list to the SymbolTable.
@@ -533,99 +589,35 @@ namespace llvm {
     /// SymbolTable to aid in emitting the DYSYMTAB load command.
     std::vector<unsigned> DynamicSymbolTable;
     
-    /// StringTable - The table of strings referenced by SymbolTable entries
-    std::vector<std::string> StringTable;
-
-    // align - Emit padding into the file until the current output position is
-    // aligned to the specified power of two boundary.
-    static void align(DataBuffer &Output, unsigned Boundary) {
-      assert(Boundary && (Boundary & (Boundary-1)) == 0 &&
-             "Must align to 2^k boundary");
-      size_t Size = Output.size();
-      if (Size & (Boundary-1)) {
-        // Add padding to get alignment to the correct place.
-        size_t Pad = Boundary-(Size & (Boundary-1));
-        Output.resize(Size+Pad);
-      }
-    }
+    static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
+                        const TargetData *TD, 
+                        std::vector<MachineRelocation> &MRs);
 
-    void outbyte(DataBuffer &Output, unsigned char X) {
-      Output.push_back(X);
-    }
-    void outhalf(DataBuffer &Output, unsigned short X) {
-      if (isLittleEndian) {
-        Output.push_back(X&255);
-        Output.push_back(X >> 8);
-      } else {
-        Output.push_back(X >> 8);
-        Output.push_back(X&255);
-      }
-    }
-    void outword(DataBuffer &Output, unsigned X) {
-      if (isLittleEndian) {
-        Output.push_back((X >>  0) & 255);
-        Output.push_back((X >>  8) & 255);
-        Output.push_back((X >> 16) & 255);
-        Output.push_back((X >> 24) & 255);
-      } else {
-        Output.push_back((X >> 24) & 255);
-        Output.push_back((X >> 16) & 255);
-        Output.push_back((X >>  8) & 255);
-        Output.push_back((X >>  0) & 255);
-      }
-    }
-    void outxword(DataBuffer &Output, uint64_t X) {
-      if (isLittleEndian) {
-        Output.push_back(unsigned(X >>  0) & 255);
-        Output.push_back(unsigned(X >>  8) & 255);
-        Output.push_back(unsigned(X >> 16) & 255);
-        Output.push_back(unsigned(X >> 24) & 255);
-        Output.push_back(unsigned(X >> 32) & 255);
-        Output.push_back(unsigned(X >> 40) & 255);
-        Output.push_back(unsigned(X >> 48) & 255);
-        Output.push_back(unsigned(X >> 56) & 255);
-      } else {
-        Output.push_back(unsigned(X >> 56) & 255);
-        Output.push_back(unsigned(X >> 48) & 255);
-        Output.push_back(unsigned(X >> 40) & 255);
-        Output.push_back(unsigned(X >> 32) & 255);
-        Output.push_back(unsigned(X >> 24) & 255);
-        Output.push_back(unsigned(X >> 16) & 255);
-        Output.push_back(unsigned(X >>  8) & 255);
-        Output.push_back(unsigned(X >>  0) & 255);
-      }
-    }
-    void outaddr32(DataBuffer &Output, unsigned X) {
-      outword(Output, X);
-    }
-    void outaddr64(DataBuffer &Output, uint64_t X) {
-      outxword(Output, X);
-    }
-    void outaddr(DataBuffer &Output, uint64_t X) {
-      if (!is64Bit)
-        outword(Output, (unsigned)X);
-      else
-        outxword(Output, X);
-    }
-    void outstring(DataBuffer &Output, std::string &S, unsigned Length) {
-      unsigned len_to_copy = S.length() < Length ? S.length() : Length;
-      unsigned len_to_fill = S.length() < Length ? Length-S.length() : 0;
-      
-      for (unsigned i = 0; i < len_to_copy; ++i)
-        outbyte(Output, S[i]);
-
-      for (unsigned i = 0; i < len_to_fill; ++i)
-        outbyte(Output, 0);
-      
-    }
   private:
-    void AddSymbolToSection(MachOSection &MOS, GlobalVariable *GV);
+    void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
     void EmitGlobal(GlobalVariable *GV);
     void EmitHeaderAndLoadCommands();
     void EmitSections();
-    void EmitRelocations();
-    void EmitSymbolTable();
-    void EmitStringTable();
+    void BufferSymbolAndStringTable();
+    void CalculateRelocations(MachOSection &MOS);
+
+    MachineRelocation GetJTRelocation(unsigned Offset,
+                                      MachineBasicBlock *MBB) const {
+      return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
+    }
+
+    /// GetTargetRelocation - Returns the number of relocations.
+    unsigned GetTargetRelocation(MachineRelocation &MR,
+                                 unsigned FromIdx,
+                                 unsigned ToAddr,
+                                 unsigned ToIndex,
+                                 OutputBuffer &RelocOut,
+                                 OutputBuffer &SecOut,
+                                 bool Scattered) {
+      return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
+                                                          ToIndex, RelocOut,
+                                                          SecOut, Scattered);
+    }
   };
 }