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