Home | History | Annotate | Download | only in MC
      1 //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_MC_MCCONTEXT_H
     11 #define LLVM_MC_MCCONTEXT_H
     12 
     13 #include "llvm/MC/SectionKind.h"
     14 #include "llvm/MC/MCDwarf.h"
     15 #include "llvm/ADT/DenseMap.h"
     16 #include "llvm/ADT/StringMap.h"
     17 #include "llvm/Support/Allocator.h"
     18 #include "llvm/Support/raw_ostream.h"
     19 #include <vector> // FIXME: Shouldn't be needed.
     20 
     21 namespace llvm {
     22   class MCAsmInfo;
     23   class MCExpr;
     24   class MCSection;
     25   class MCSymbol;
     26   class MCLabel;
     27   class MCDwarfFile;
     28   class MCDwarfLoc;
     29   class MCObjectFileInfo;
     30   class MCRegisterInfo;
     31   class MCLineSection;
     32   class StringRef;
     33   class Twine;
     34   class MCSectionMachO;
     35   class MCSectionELF;
     36 
     37   /// MCContext - Context object for machine code objects.  This class owns all
     38   /// of the sections that it creates.
     39   ///
     40   class MCContext {
     41     MCContext(const MCContext&); // DO NOT IMPLEMENT
     42     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
     43   public:
     44     typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
     45   private:
     46 
     47     /// The MCAsmInfo for this target.
     48     const MCAsmInfo &MAI;
     49 
     50     /// The MCRegisterInfo for this target.
     51     const MCRegisterInfo &MRI;
     52 
     53     /// The MCObjectFileInfo for this target.
     54     const MCObjectFileInfo *MOFI;
     55 
     56     /// Allocator - Allocator object used for creating machine code objects.
     57     ///
     58     /// We use a bump pointer allocator to avoid the need to track all allocated
     59     /// objects.
     60     BumpPtrAllocator Allocator;
     61 
     62     /// Symbols - Bindings of names to symbols.
     63     SymbolTable Symbols;
     64 
     65     /// UsedNames - Keeps tracks of names that were used both for used declared
     66     /// and artificial symbols.
     67     StringMap<bool, BumpPtrAllocator&> UsedNames;
     68 
     69     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
     70     /// symbol.
     71     unsigned NextUniqueID;
     72 
     73     /// Instances of directional local labels.
     74     DenseMap<unsigned, MCLabel *> Instances;
     75     /// NextInstance() creates the next instance of the directional local label
     76     /// for the LocalLabelVal and adds it to the map if needed.
     77     unsigned NextInstance(int64_t LocalLabelVal);
     78     /// GetInstance() gets the current instance of the directional local label
     79     /// for the LocalLabelVal and adds it to the map if needed.
     80     unsigned GetInstance(int64_t LocalLabelVal);
     81 
     82     /// The file name of the log file from the environment variable
     83     /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
     84     /// directive is used or it is an error.
     85     char *SecureLogFile;
     86     /// The stream that gets written to for the .secure_log_unique directive.
     87     raw_ostream *SecureLog;
     88     /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
     89     /// catch errors if .secure_log_unique appears twice without
     90     /// .secure_log_reset appearing between them.
     91     bool SecureLogUsed;
     92 
     93     /// The dwarf file and directory tables from the dwarf .file directive.
     94     std::vector<MCDwarfFile *> MCDwarfFiles;
     95     std::vector<StringRef> MCDwarfDirs;
     96 
     97     /// The current dwarf line information from the last dwarf .loc directive.
     98     MCDwarfLoc CurrentDwarfLoc;
     99     bool DwarfLocSeen;
    100 
    101     /// Honor temporary labels, this is useful for debugging semantic
    102     /// differences between temporary and non-temporary labels (primarily on
    103     /// Darwin).
    104     bool AllowTemporaryLabels;
    105 
    106     /// The dwarf line information from the .loc directives for the sections
    107     /// with assembled machine instructions have after seeing .loc directives.
    108     DenseMap<const MCSection *, MCLineSection *> MCLineSections;
    109     /// We need a deterministic iteration order, so we remember the order
    110     /// the elements were added.
    111     std::vector<const MCSection *> MCLineSectionOrder;
    112 
    113     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
    114 
    115     MCSymbol *CreateSymbol(StringRef Name);
    116 
    117   public:
    118     explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
    119                        const MCObjectFileInfo *MOFI);
    120     ~MCContext();
    121 
    122     const MCAsmInfo &getAsmInfo() const { return MAI; }
    123 
    124     const MCRegisterInfo &getRegisterInfo() const { return MRI; }
    125 
    126     const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
    127 
    128     void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
    129 
    130     /// @name Symbol Management
    131     /// @{
    132 
    133     /// CreateTempSymbol - Create and return a new assembler temporary symbol
    134     /// with a unique but unspecified name.
    135     MCSymbol *CreateTempSymbol();
    136 
    137     /// CreateDirectionalLocalSymbol - Create the definition of a directional
    138     /// local symbol for numbered label (used for "1:" definitions).
    139     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
    140 
    141     /// GetDirectionalLocalSymbol - Create and return a directional local
    142     /// symbol for numbered label (used for "1b" or 1f" references).
    143     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
    144 
    145     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
    146     /// @p Name.  If it exists, return it.  If not, create a forward
    147     /// reference and return it.
    148     ///
    149     /// @param Name - The symbol name, which must be unique across all symbols.
    150     MCSymbol *GetOrCreateSymbol(StringRef Name);
    151     MCSymbol *GetOrCreateSymbol(const Twine &Name);
    152 
    153     /// LookupSymbol - Get the symbol for \p Name, or null.
    154     MCSymbol *LookupSymbol(StringRef Name) const;
    155 
    156     /// getSymbols - Get a reference for the symbol table for clients that
    157     /// want to, for example, iterate over all symbols. 'const' because we
    158     /// still want any modifications to the table itself to use the MCContext
    159     /// APIs.
    160     const SymbolTable &getSymbols() const {
    161       return Symbols;
    162     }
    163 
    164     /// @}
    165 
    166     /// @name Section Management
    167     /// @{
    168 
    169     /// getMachOSection - Return the MCSection for the specified mach-o section.
    170     /// This requires the operands to be valid.
    171     const MCSectionMachO *getMachOSection(StringRef Segment,
    172                                           StringRef Section,
    173                                           unsigned TypeAndAttributes,
    174                                           unsigned Reserved2,
    175                                           SectionKind K);
    176     const MCSectionMachO *getMachOSection(StringRef Segment,
    177                                           StringRef Section,
    178                                           unsigned TypeAndAttributes,
    179                                           SectionKind K) {
    180       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
    181     }
    182 
    183     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
    184                                       unsigned Flags, SectionKind Kind);
    185 
    186     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
    187                                       unsigned Flags, SectionKind Kind,
    188                                       unsigned EntrySize, StringRef Group);
    189 
    190     const MCSectionELF *CreateELFGroupSection();
    191 
    192     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
    193                                     int Selection, SectionKind Kind);
    194 
    195     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
    196                                     SectionKind Kind) {
    197       return getCOFFSection (Section, Characteristics, 0, Kind);
    198     }
    199 
    200 
    201     /// @}
    202 
    203     /// @name Dwarf Management
    204     /// @{
    205 
    206     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
    207     unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
    208                           unsigned FileNumber);
    209 
    210     bool isValidDwarfFileNumber(unsigned FileNumber);
    211 
    212     bool hasDwarfFiles() const {
    213       return !MCDwarfFiles.empty();
    214     }
    215 
    216     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
    217       return MCDwarfFiles;
    218     }
    219     const std::vector<StringRef> &getMCDwarfDirs() {
    220       return MCDwarfDirs;
    221     }
    222 
    223     const DenseMap<const MCSection *, MCLineSection *>
    224     &getMCLineSections() const {
    225       return MCLineSections;
    226     }
    227     const std::vector<const MCSection *> &getMCLineSectionOrder() const {
    228       return MCLineSectionOrder;
    229     }
    230     void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
    231       MCLineSections[Sec] = Line;
    232       MCLineSectionOrder.push_back(Sec);
    233     }
    234 
    235     /// setCurrentDwarfLoc - saves the information from the currently parsed
    236     /// dwarf .loc directive and sets DwarfLocSeen.  When the next instruction
    237     /// is assembled an entry in the line number table with this information and
    238     /// the address of the instruction will be created.
    239     void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
    240                             unsigned Flags, unsigned Isa,
    241                             unsigned Discriminator) {
    242       CurrentDwarfLoc.setFileNum(FileNum);
    243       CurrentDwarfLoc.setLine(Line);
    244       CurrentDwarfLoc.setColumn(Column);
    245       CurrentDwarfLoc.setFlags(Flags);
    246       CurrentDwarfLoc.setIsa(Isa);
    247       CurrentDwarfLoc.setDiscriminator(Discriminator);
    248       DwarfLocSeen = true;
    249     }
    250     void ClearDwarfLocSeen() { DwarfLocSeen = false; }
    251 
    252     bool getDwarfLocSeen() { return DwarfLocSeen; }
    253     const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
    254 
    255     /// @}
    256 
    257     char *getSecureLogFile() { return SecureLogFile; }
    258     raw_ostream *getSecureLog() { return SecureLog; }
    259     bool getSecureLogUsed() { return SecureLogUsed; }
    260     void setSecureLog(raw_ostream *Value) {
    261       SecureLog = Value;
    262     }
    263     void setSecureLogUsed(bool Value) {
    264       SecureLogUsed = Value;
    265     }
    266 
    267     void *Allocate(unsigned Size, unsigned Align = 8) {
    268       return Allocator.Allocate(Size, Align);
    269     }
    270     void Deallocate(void *Ptr) {
    271     }
    272   };
    273 
    274 } // end namespace llvm
    275 
    276 // operator new and delete aren't allowed inside namespaces.
    277 // The throw specifications are mandated by the standard.
    278 /// @brief Placement new for using the MCContext's allocator.
    279 ///
    280 /// This placement form of operator new uses the MCContext's allocator for
    281 /// obtaining memory. It is a non-throwing new, which means that it returns
    282 /// null on error. (If that is what the allocator does. The current does, so if
    283 /// this ever changes, this operator will have to be changed, too.)
    284 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
    285 /// @code
    286 /// // Default alignment (16)
    287 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
    288 /// // Specific alignment
    289 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
    290 /// @endcode
    291 /// Please note that you cannot use delete on the pointer; it must be
    292 /// deallocated using an explicit destructor call followed by
    293 /// @c Context.Deallocate(Ptr).
    294 ///
    295 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
    296 /// @param C The MCContext that provides the allocator.
    297 /// @param Alignment The alignment of the allocated memory (if the underlying
    298 ///                  allocator supports it).
    299 /// @return The allocated memory. Could be NULL.
    300 inline void *operator new(size_t Bytes, llvm::MCContext &C,
    301                           size_t Alignment = 16) throw () {
    302   return C.Allocate(Bytes, Alignment);
    303 }
    304 /// @brief Placement delete companion to the new above.
    305 ///
    306 /// This operator is just a companion to the new above. There is no way of
    307 /// invoking it directly; see the new operator for more details. This operator
    308 /// is called implicitly by the compiler if a placement new expression using
    309 /// the MCContext throws in the object constructor.
    310 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
    311               throw () {
    312   C.Deallocate(Ptr);
    313 }
    314 
    315 /// This placement form of operator new[] uses the MCContext's allocator for
    316 /// obtaining memory. It is a non-throwing new[], which means that it returns
    317 /// null on error.
    318 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
    319 /// @code
    320 /// // Default alignment (16)
    321 /// char *data = new (Context) char[10];
    322 /// // Specific alignment
    323 /// char *data = new (Context, 8) char[10];
    324 /// @endcode
    325 /// Please note that you cannot use delete on the pointer; it must be
    326 /// deallocated using an explicit destructor call followed by
    327 /// @c Context.Deallocate(Ptr).
    328 ///
    329 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
    330 /// @param C The MCContext that provides the allocator.
    331 /// @param Alignment The alignment of the allocated memory (if the underlying
    332 ///                  allocator supports it).
    333 /// @return The allocated memory. Could be NULL.
    334 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
    335                             size_t Alignment = 16) throw () {
    336   return C.Allocate(Bytes, Alignment);
    337 }
    338 
    339 /// @brief Placement delete[] companion to the new[] above.
    340 ///
    341 /// This operator is just a companion to the new[] above. There is no way of
    342 /// invoking it directly; see the new[] operator for more details. This operator
    343 /// is called implicitly by the compiler if a placement new[] expression using
    344 /// the MCContext throws in the object constructor.
    345 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
    346   C.Deallocate(Ptr);
    347 }
    348 
    349 #endif
    350