Home | History | Annotate | Download | only in CodeGen
      1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- 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 // This file contains a class to be used as the base class for target specific
     11 // asm writers.  This class primarily handles common functionality used by
     12 // all asm writers.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
     17 #define LLVM_CODEGEN_ASMPRINTER_H
     18 
     19 #include "llvm/ADT/MapVector.h"
     20 #include "llvm/ADT/Twine.h"
     21 #include "llvm/CodeGen/MachineFunctionPass.h"
     22 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
     23 #include "llvm/IR/InlineAsm.h"
     24 #include "llvm/Support/DataTypes.h"
     25 #include "llvm/Support/ErrorHandling.h"
     26 
     27 namespace llvm {
     28 class AsmPrinterHandler;
     29 class BlockAddress;
     30 class ByteStreamer;
     31 class GCStrategy;
     32 class Constant;
     33 class ConstantArray;
     34 class DIE;
     35 class DIEAbbrev;
     36 class GCMetadataPrinter;
     37 class GlobalIndirectSymbol;
     38 class GlobalValue;
     39 class GlobalVariable;
     40 class MachineBasicBlock;
     41 class MachineFunction;
     42 class MachineInstr;
     43 class MachineLocation;
     44 class MachineLoopInfo;
     45 class MachineLoop;
     46 class MachineConstantPoolValue;
     47 class MachineJumpTableInfo;
     48 class MachineModuleInfo;
     49 class MCAsmInfo;
     50 class MCCFIInstruction;
     51 class MCContext;
     52 class MCExpr;
     53 class MCInst;
     54 class MCSection;
     55 class MCStreamer;
     56 class MCSubtargetInfo;
     57 class MCSymbol;
     58 class MCTargetOptions;
     59 class MDNode;
     60 class DwarfDebug;
     61 class Mangler;
     62 class TargetLoweringObjectFile;
     63 class DataLayout;
     64 class TargetMachine;
     65 
     66 /// This class is intended to be used as a driving class for all asm writers.
     67 class AsmPrinter : public MachineFunctionPass {
     68 public:
     69   /// Target machine description.
     70   ///
     71   TargetMachine &TM;
     72 
     73   /// Target Asm Printer information.
     74   ///
     75   const MCAsmInfo *MAI;
     76 
     77   /// This is the context for the output file that we are streaming. This owns
     78   /// all of the global MC-related objects for the generated translation unit.
     79   MCContext &OutContext;
     80 
     81   /// This is the MCStreamer object for the file we are generating. This
     82   /// contains the transient state for the current translation unit that we are
     83   /// generating (such as the current section etc).
     84   std::unique_ptr<MCStreamer> OutStreamer;
     85 
     86   /// The current machine function.
     87   const MachineFunction *MF;
     88 
     89   /// This is a pointer to the current MachineModuleInfo.
     90   MachineModuleInfo *MMI;
     91 
     92   /// Name-mangler for global names.
     93   ///
     94   Mangler *Mang;
     95 
     96   /// The symbol for the current function. This is recalculated at the beginning
     97   /// of each call to runOnMachineFunction().
     98   ///
     99   MCSymbol *CurrentFnSym;
    100 
    101   /// The symbol used to represent the start of the current function for the
    102   /// purpose of calculating its size (e.g. using the .size directive). By
    103   /// default, this is equal to CurrentFnSym.
    104   MCSymbol *CurrentFnSymForSize;
    105 
    106   /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
    107   /// its number of uses by other globals.
    108   typedef std::pair<const GlobalVariable *, unsigned> GOTEquivUsePair;
    109   MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
    110 
    111 private:
    112   MCSymbol *CurrentFnBegin;
    113   MCSymbol *CurrentFnEnd;
    114   MCSymbol *CurExceptionSym;
    115 
    116   // The garbage collection metadata printer table.
    117   void *GCMetadataPrinters; // Really a DenseMap.
    118 
    119   /// Emit comments in assembly output if this is true.
    120   ///
    121   bool VerboseAsm;
    122   static char ID;
    123 
    124   /// If VerboseAsm is set, a pointer to the loop info for this function.
    125   MachineLoopInfo *LI;
    126 
    127   struct HandlerInfo {
    128     AsmPrinterHandler *Handler;
    129     const char *TimerName, *TimerGroupName;
    130     HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
    131                 const char *TimerGroupName)
    132         : Handler(Handler), TimerName(TimerName),
    133           TimerGroupName(TimerGroupName) {}
    134   };
    135   /// A vector of all debug/EH info emitters we should use. This vector
    136   /// maintains ownership of the emitters.
    137   SmallVector<HandlerInfo, 1> Handlers;
    138 
    139   /// If the target supports dwarf debug info, this pointer is non-null.
    140   DwarfDebug *DD;
    141 
    142 protected:
    143   explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
    144 
    145 public:
    146   ~AsmPrinter() override;
    147 
    148   DwarfDebug *getDwarfDebug() { return DD; }
    149   DwarfDebug *getDwarfDebug() const { return DD; }
    150 
    151   bool isPositionIndependent() const;
    152 
    153   /// Return true if assembly output should contain comments.
    154   ///
    155   bool isVerbose() const { return VerboseAsm; }
    156 
    157   /// Return a unique ID for the current function.
    158   ///
    159   unsigned getFunctionNumber() const;
    160 
    161   MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
    162   MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
    163   MCSymbol *getCurExceptionSym();
    164 
    165   /// Return information about object file lowering.
    166   const TargetLoweringObjectFile &getObjFileLowering() const;
    167 
    168   /// Return information about data layout.
    169   const DataLayout &getDataLayout() const;
    170 
    171   /// Return the pointer size from the TargetMachine
    172   unsigned getPointerSize() const;
    173 
    174   /// Return information about subtarget.
    175   const MCSubtargetInfo &getSubtargetInfo() const;
    176 
    177   void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
    178 
    179   /// Return the target triple string.
    180   StringRef getTargetTriple() const;
    181 
    182   /// Return the current section we are emitting to.
    183   const MCSection *getCurrentSection() const;
    184 
    185   void getNameWithPrefix(SmallVectorImpl<char> &Name,
    186                          const GlobalValue *GV) const;
    187 
    188   MCSymbol *getSymbol(const GlobalValue *GV) const;
    189 
    190   //===------------------------------------------------------------------===//
    191   // MachineFunctionPass Implementation.
    192   //===------------------------------------------------------------------===//
    193 
    194   /// Record analysis usage.
    195   ///
    196   void getAnalysisUsage(AnalysisUsage &AU) const override;
    197 
    198   /// Set up the AsmPrinter when we are working on a new module. If your pass
    199   /// overrides this, it must make sure to explicitly call this implementation.
    200   bool doInitialization(Module &M) override;
    201 
    202   /// Shut down the asmprinter. If you override this in your pass, you must make
    203   /// sure to call it explicitly.
    204   bool doFinalization(Module &M) override;
    205 
    206   /// Emit the specified function out to the OutStreamer.
    207   bool runOnMachineFunction(MachineFunction &MF) override {
    208     SetupMachineFunction(MF);
    209     EmitFunctionBody();
    210     return false;
    211   }
    212 
    213   //===------------------------------------------------------------------===//
    214   // Coarse grained IR lowering routines.
    215   //===------------------------------------------------------------------===//
    216 
    217   /// This should be called when a new MachineFunction is being processed from
    218   /// runOnMachineFunction.
    219   void SetupMachineFunction(MachineFunction &MF);
    220 
    221   /// This method emits the body and trailer for a function.
    222   void EmitFunctionBody();
    223 
    224   void emitCFIInstruction(const MachineInstr &MI);
    225 
    226   void emitFrameAlloc(const MachineInstr &MI);
    227 
    228   enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
    229   CFIMoveType needsCFIMoves();
    230 
    231   bool needsSEHMoves();
    232 
    233   /// Print to the current output stream assembly representations of the
    234   /// constants in the constant pool MCP. This is used to print out constants
    235   /// which have been "spilled to memory" by the code generator.
    236   ///
    237   virtual void EmitConstantPool();
    238 
    239   /// Print assembly representations of the jump tables used by the current
    240   /// function to the current output stream.
    241   ///
    242   virtual void EmitJumpTableInfo();
    243 
    244   /// Emit the specified global variable to the .s file.
    245   virtual void EmitGlobalVariable(const GlobalVariable *GV);
    246 
    247   /// Check to see if the specified global is a special global used by LLVM. If
    248   /// so, emit it and return true, otherwise do nothing and return false.
    249   bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
    250 
    251   /// Emit an alignment directive to the specified power of two boundary. For
    252   /// example, if you pass in 3 here, you will get an 8 byte alignment. If a
    253   /// global value is specified, and if that global has an explicit alignment
    254   /// requested, it will override the alignment request if required for
    255   /// correctness.
    256   ///
    257   void EmitAlignment(unsigned NumBits, const GlobalObject *GO = nullptr) const;
    258 
    259   /// Lower the specified LLVM Constant to an MCExpr.
    260   virtual const MCExpr *lowerConstant(const Constant *CV);
    261 
    262   /// \brief Print a general LLVM constant to the .s file.
    263   void EmitGlobalConstant(const DataLayout &DL, const Constant *CV);
    264 
    265   /// \brief Unnamed constant global variables solely contaning a pointer to
    266   /// another globals variable act like a global variable "proxy", or GOT
    267   /// equivalents, i.e., it's only used to hold the address of the latter. One
    268   /// optimization is to replace accesses to these proxies by using the GOT
    269   /// entry for the final global instead. Hence, we select GOT equivalent
    270   /// candidates among all the module global variables, avoid emitting them
    271   /// unnecessarily and finally replace references to them by pc relative
    272   /// accesses to GOT entries.
    273   void computeGlobalGOTEquivs(Module &M);
    274 
    275   /// \brief Constant expressions using GOT equivalent globals may not be
    276   /// eligible for PC relative GOT entry conversion, in such cases we need to
    277   /// emit the proxies we previously omitted in EmitGlobalVariable.
    278   void emitGlobalGOTEquivs();
    279 
    280   //===------------------------------------------------------------------===//
    281   // Overridable Hooks
    282   //===------------------------------------------------------------------===//
    283 
    284   // Targets can, or in the case of EmitInstruction, must implement these to
    285   // customize output.
    286 
    287   /// This virtual method can be overridden by targets that want to emit
    288   /// something at the start of their file.
    289   virtual void EmitStartOfAsmFile(Module &) {}
    290 
    291   /// This virtual method can be overridden by targets that want to emit
    292   /// something at the end of their file.
    293   virtual void EmitEndOfAsmFile(Module &) {}
    294 
    295   /// Targets can override this to emit stuff before the first basic block in
    296   /// the function.
    297   virtual void EmitFunctionBodyStart() {}
    298 
    299   /// Targets can override this to emit stuff after the last basic block in the
    300   /// function.
    301   virtual void EmitFunctionBodyEnd() {}
    302 
    303   /// Targets can override this to emit stuff at the start of a basic block.
    304   /// By default, this method prints the label for the specified
    305   /// MachineBasicBlock, an alignment (if present) and a comment describing it
    306   /// if appropriate.
    307   virtual void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
    308 
    309   /// Targets can override this to emit stuff at the end of a basic block.
    310   virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB) {}
    311 
    312   /// Targets should implement this to emit instructions.
    313   virtual void EmitInstruction(const MachineInstr *) {
    314     llvm_unreachable("EmitInstruction not implemented");
    315   }
    316 
    317   /// Return the symbol for the specified constant pool entry.
    318   virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
    319 
    320   virtual void EmitFunctionEntryLabel();
    321 
    322   virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
    323 
    324   /// Targets can override this to change how global constants that are part of
    325   /// a C++ static/global constructor list are emitted.
    326   virtual void EmitXXStructor(const DataLayout &DL, const Constant *CV) {
    327     EmitGlobalConstant(DL, CV);
    328   }
    329 
    330   /// Return true if the basic block has exactly one predecessor and the control
    331   /// transfer mechanism between the predecessor and this block is a
    332   /// fall-through.
    333   virtual bool
    334   isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
    335 
    336   /// Targets can override this to customize the output of IMPLICIT_DEF
    337   /// instructions in verbose mode.
    338   virtual void emitImplicitDef(const MachineInstr *MI) const;
    339 
    340   //===------------------------------------------------------------------===//
    341   // Symbol Lowering Routines.
    342   //===------------------------------------------------------------------===//
    343 public:
    344   MCSymbol *createTempSymbol(const Twine &Name) const;
    345 
    346   /// Return the MCSymbol for a private symbol with global value name as its
    347   /// base, with the specified suffix.
    348   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
    349                                          StringRef Suffix) const;
    350 
    351   /// Return the MCSymbol for the specified ExternalSymbol.
    352   MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
    353 
    354   /// Return the symbol for the specified jump table entry.
    355   MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
    356 
    357   /// Return the symbol for the specified jump table .set
    358   /// FIXME: privatize to AsmPrinter.
    359   MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
    360 
    361   /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
    362   /// basic block.
    363   MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
    364   MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
    365 
    366   //===------------------------------------------------------------------===//
    367   // Emission Helper Routines.
    368   //===------------------------------------------------------------------===//
    369 public:
    370   /// This is just convenient handler for printing offsets.
    371   void printOffset(int64_t Offset, raw_ostream &OS) const;
    372 
    373   /// Emit a byte directive and value.
    374   ///
    375   void EmitInt8(int Value) const;
    376 
    377   /// Emit a short directive and value.
    378   ///
    379   void EmitInt16(int Value) const;
    380 
    381   /// Emit a long directive and value.
    382   ///
    383   void EmitInt32(int Value) const;
    384 
    385   /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
    386   /// is specified by Size and Hi/Lo specify the labels.  This implicitly uses
    387   /// .set if it is available.
    388   void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
    389                            unsigned Size) const;
    390 
    391   /// Emit something like ".long Label+Offset" where the size in bytes of the
    392   /// directive is specified by Size and Label specifies the label.  This
    393   /// implicitly uses .set if it is available.
    394   void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
    395                            unsigned Size, bool IsSectionRelative = false) const;
    396 
    397   /// Emit something like ".long Label" where the size in bytes of the directive
    398   /// is specified by Size and Label specifies the label.
    399   void EmitLabelReference(const MCSymbol *Label, unsigned Size,
    400                           bool IsSectionRelative = false) const {
    401     EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
    402   }
    403 
    404   //===------------------------------------------------------------------===//
    405   // Dwarf Emission Helper Routines
    406   //===------------------------------------------------------------------===//
    407 
    408   /// Emit the specified signed leb128 value.
    409   void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
    410 
    411   /// Emit the specified unsigned leb128 value.
    412   void EmitULEB128(uint64_t Value, const char *Desc = nullptr,
    413                    unsigned PadTo = 0) const;
    414 
    415   /// Emit a .byte 42 directive that corresponds to an encoding.  If verbose
    416   /// assembly output is enabled, we output comments describing the encoding.
    417   /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
    418   void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
    419 
    420   /// Return the size of the encoding in bytes.
    421   unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
    422 
    423   /// Emit reference to a ttype global with a specified encoding.
    424   void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
    425 
    426   /// Emit a reference to a symbol for use in dwarf. Different object formats
    427   /// represent this in different ways. Some use a relocation others encode
    428   /// the label offset in its section.
    429   void emitDwarfSymbolReference(const MCSymbol *Label,
    430                                 bool ForceOffset = false) const;
    431 
    432   /// Emit the 4-byte offset of a string from the start of its section.
    433   ///
    434   /// When possible, emit a DwarfStringPool section offset without any
    435   /// relocations, and without using the symbol.  Otherwise, defers to \a
    436   /// emitDwarfSymbolReference().
    437   void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const;
    438 
    439   /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
    440   virtual unsigned getISAEncoding() { return 0; }
    441 
    442   /// EmitDwarfRegOp - Emit a dwarf register operation.
    443   virtual void EmitDwarfRegOp(ByteStreamer &BS,
    444                               const MachineLocation &MLoc) const;
    445 
    446   //===------------------------------------------------------------------===//
    447   // Dwarf Lowering Routines
    448   //===------------------------------------------------------------------===//
    449 
    450   /// \brief Emit frame instruction to describe the layout of the frame.
    451   void emitCFIInstruction(const MCCFIInstruction &Inst) const;
    452 
    453   /// \brief Emit Dwarf abbreviation table.
    454   template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
    455     // For each abbreviation.
    456     for (const auto &Abbrev : Abbrevs)
    457       emitDwarfAbbrev(*Abbrev);
    458 
    459     // Mark end of abbreviations.
    460     EmitULEB128(0, "EOM(3)");
    461   }
    462 
    463   void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
    464 
    465   /// \brief Recursively emit Dwarf DIE tree.
    466   void emitDwarfDIE(const DIE &Die) const;
    467 
    468   //===------------------------------------------------------------------===//
    469   // Inline Asm Support
    470   //===------------------------------------------------------------------===//
    471 public:
    472   // These are hooks that targets can override to implement inline asm
    473   // support.  These should probably be moved out of AsmPrinter someday.
    474 
    475   /// Print information related to the specified machine instr that is
    476   /// independent of the operand, and may be independent of the instr itself.
    477   /// This can be useful for portably encoding the comment character or other
    478   /// bits of target-specific knowledge into the asmstrings.  The syntax used is
    479   /// ${:comment}.  Targets can override this to add support for their own
    480   /// strange codes.
    481   virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
    482                             const char *Code) const;
    483 
    484   /// Print the specified operand of MI, an INLINEASM instruction, using the
    485   /// specified assembler variant.  Targets should override this to format as
    486   /// appropriate.  This method can return true if the operand is erroneous.
    487   virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
    488                                unsigned AsmVariant, const char *ExtraCode,
    489                                raw_ostream &OS);
    490 
    491   /// Print the specified operand of MI, an INLINEASM instruction, using the
    492   /// specified assembler variant as an address. Targets should override this to
    493   /// format as appropriate.  This method can return true if the operand is
    494   /// erroneous.
    495   virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
    496                                      unsigned AsmVariant, const char *ExtraCode,
    497                                      raw_ostream &OS);
    498 
    499   /// Let the target do anything it needs to do before emitting inlineasm.
    500   /// \p StartInfo - the subtarget info before parsing inline asm
    501   virtual void emitInlineAsmStart() const;
    502 
    503   /// Let the target do anything it needs to do after emitting inlineasm.
    504   /// This callback can be used restore the original mode in case the
    505   /// inlineasm contains directives to switch modes.
    506   /// \p StartInfo - the original subtarget info before inline asm
    507   /// \p EndInfo   - the final subtarget info after parsing the inline asm,
    508   ///                or NULL if the value is unknown.
    509   virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
    510                                 const MCSubtargetInfo *EndInfo) const;
    511 
    512 private:
    513   /// Private state for PrintSpecial()
    514   // Assign a unique ID to this machine instruction.
    515   mutable const MachineInstr *LastMI;
    516   mutable unsigned LastFn;
    517   mutable unsigned Counter;
    518 
    519   /// This method emits the header for the current function.
    520   virtual void EmitFunctionHeader();
    521 
    522   /// Emit a blob of inline asm to the output streamer.
    523   void
    524   EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
    525                 const MCTargetOptions &MCOptions,
    526                 const MDNode *LocMDNode = nullptr,
    527                 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
    528 
    529   /// This method formats and emits the specified machine instruction that is an
    530   /// inline asm.
    531   void EmitInlineAsm(const MachineInstr *MI) const;
    532 
    533   //===------------------------------------------------------------------===//
    534   // Internal Implementation Details
    535   //===------------------------------------------------------------------===//
    536 
    537   /// This emits visibility information about symbol, if this is suported by the
    538   /// target.
    539   void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
    540                       bool IsDefinition = true) const;
    541 
    542   void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
    543 
    544   void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
    545                           const MachineBasicBlock *MBB, unsigned uid) const;
    546   void EmitLLVMUsedList(const ConstantArray *InitList);
    547   /// Emit llvm.ident metadata in an '.ident' directive.
    548   void EmitModuleIdents(Module &M);
    549   void EmitXXStructorList(const DataLayout &DL, const Constant *List,
    550                           bool isCtor);
    551   GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &C);
    552   /// Emit GlobalAlias or GlobalIFunc.
    553   void emitGlobalIndirectSymbol(Module &M,
    554                                 const GlobalIndirectSymbol& GIS);
    555 };
    556 }
    557 
    558 #endif
    559