LLVM API Documentation

AsmPrinter.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a class to be used as the base class for target specific
00011 // asm writers.  This class primarily handles common functionality used by
00012 // all asm writers.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_ASMPRINTER_H
00017 #define LLVM_CODEGEN_ASMPRINTER_H
00018 
00019 #include "llvm/ADT/Twine.h"
00020 #include "llvm/CodeGen/MachineFunctionPass.h"
00021 #include "llvm/IR/InlineAsm.h"
00022 #include "llvm/Support/DataTypes.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 
00025 namespace llvm {
00026 class AsmPrinterHandler;
00027 class BlockAddress;
00028 class ByteStreamer;
00029 class GCStrategy;
00030 class Constant;
00031 class ConstantArray;
00032 class GCMetadataPrinter;
00033 class GlobalValue;
00034 class GlobalVariable;
00035 class MachineBasicBlock;
00036 class MachineFunction;
00037 class MachineInstr;
00038 class MachineLocation;
00039 class MachineLoopInfo;
00040 class MachineLoop;
00041 class MachineConstantPoolValue;
00042 class MachineJumpTableInfo;
00043 class MachineModuleInfo;
00044 class MCAsmInfo;
00045 class MCCFIInstruction;
00046 class MCContext;
00047 class MCInst;
00048 class MCInstrInfo;
00049 class MCSection;
00050 class MCStreamer;
00051 class MCSubtargetInfo;
00052 class MCSymbol;
00053 class MDNode;
00054 class DwarfDebug;
00055 class Mangler;
00056 class TargetLoweringObjectFile;
00057 class DataLayout;
00058 class TargetMachine;
00059 
00060 /// This class is intended to be used as a driving class for all asm writers.
00061 class AsmPrinter : public MachineFunctionPass {
00062 public:
00063   /// Target machine description.
00064   ///
00065   TargetMachine &TM;
00066 
00067   /// Target Asm Printer information.
00068   ///
00069   const MCAsmInfo *MAI;
00070 
00071   const MCInstrInfo *MII;
00072   /// This is the context for the output file that we are streaming. This owns
00073   /// all of the global MC-related objects for the generated translation unit.
00074   MCContext &OutContext;
00075 
00076   /// This is the MCStreamer object for the file we are generating. This
00077   /// contains the transient state for the current translation unit that we are
00078   /// generating (such as the current section etc).
00079   MCStreamer &OutStreamer;
00080 
00081   /// The current machine function.
00082   const MachineFunction *MF;
00083 
00084   /// This is a pointer to the current MachineModuleInfo.
00085   MachineModuleInfo *MMI;
00086 
00087   /// Name-mangler for global names.
00088   ///
00089   Mangler *Mang;
00090 
00091   /// The symbol for the current function. This is recalculated at the beginning
00092   /// of each call to runOnMachineFunction().
00093   ///
00094   MCSymbol *CurrentFnSym;
00095 
00096   /// The symbol used to represent the start of the current function for the
00097   /// purpose of calculating its size (e.g. using the .size directive). By
00098   /// default, this is equal to CurrentFnSym.
00099   MCSymbol *CurrentFnSymForSize;
00100 
00101 private:
00102   // The garbage collection metadata printer table.
00103   void *GCMetadataPrinters; // Really a DenseMap.
00104 
00105   /// Emit comments in assembly output if this is true.
00106   ///
00107   bool VerboseAsm;
00108   static char ID;
00109 
00110   /// If VerboseAsm is set, a pointer to the loop info for this function.
00111   MachineLoopInfo *LI;
00112 
00113   struct HandlerInfo {
00114     AsmPrinterHandler *Handler;
00115     const char *TimerName, *TimerGroupName;
00116     HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
00117                 const char *TimerGroupName)
00118         : Handler(Handler), TimerName(TimerName),
00119           TimerGroupName(TimerGroupName) {}
00120   };
00121   /// A vector of all debug/EH info emitters we should use. This vector
00122   /// maintains ownership of the emitters.
00123   SmallVector<HandlerInfo, 1> Handlers;
00124 
00125   /// If the target supports dwarf debug info, this pointer is non-null.
00126   DwarfDebug *DD;
00127 
00128 protected:
00129   explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
00130 
00131 public:
00132   virtual ~AsmPrinter();
00133 
00134   DwarfDebug *getDwarfDebug() { return DD; }
00135 
00136   /// Return true if assembly output should contain comments.
00137   ///
00138   bool isVerbose() const { return VerboseAsm; }
00139 
00140   /// Return a unique ID for the current function.
00141   ///
00142   unsigned getFunctionNumber() const;
00143 
00144   /// Return information about object file lowering.
00145   const TargetLoweringObjectFile &getObjFileLowering() const;
00146 
00147   /// Return information about data layout.
00148   const DataLayout &getDataLayout() const;
00149 
00150   /// Return information about subtarget.
00151   const MCSubtargetInfo &getSubtargetInfo() const;
00152 
00153   void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
00154 
00155   /// Return the target triple string.
00156   StringRef getTargetTriple() const;
00157 
00158   /// Return the current section we are emitting to.
00159   const MCSection *getCurrentSection() const;
00160 
00161   void getNameWithPrefix(SmallVectorImpl<char> &Name,
00162                          const GlobalValue *GV) const;
00163 
00164   MCSymbol *getSymbol(const GlobalValue *GV) const;
00165 
00166   //===------------------------------------------------------------------===//
00167   // MachineFunctionPass Implementation.
00168   //===------------------------------------------------------------------===//
00169 
00170   /// Record analysis usage.
00171   ///
00172   void getAnalysisUsage(AnalysisUsage &AU) const override;
00173 
00174   /// Set up the AsmPrinter when we are working on a new module. If your pass
00175   /// overrides this, it must make sure to explicitly call this implementation.
00176   bool doInitialization(Module &M) override;
00177 
00178   /// Shut down the asmprinter. If you override this in your pass, you must make
00179   /// sure to call it explicitly.
00180   bool doFinalization(Module &M) override;
00181 
00182   /// Emit the specified function out to the OutStreamer.
00183   bool runOnMachineFunction(MachineFunction &MF) override {
00184     SetupMachineFunction(MF);
00185     EmitFunctionHeader();
00186     EmitFunctionBody();
00187     return false;
00188   }
00189 
00190   //===------------------------------------------------------------------===//
00191   // Coarse grained IR lowering routines.
00192   //===------------------------------------------------------------------===//
00193 
00194   /// This should be called when a new MachineFunction is being processed from
00195   /// runOnMachineFunction.
00196   void SetupMachineFunction(MachineFunction &MF);
00197 
00198   /// This method emits the header for the current function.
00199   void EmitFunctionHeader();
00200 
00201   /// This method emits the body and trailer for a function.
00202   void EmitFunctionBody();
00203 
00204   void emitCFIInstruction(const MachineInstr &MI);
00205 
00206   enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
00207   CFIMoveType needsCFIMoves();
00208 
00209   bool needsSEHMoves();
00210 
00211   /// Print to the current output stream assembly representations of the
00212   /// constants in the constant pool MCP. This is used to print out constants
00213   /// which have been "spilled to memory" by the code generator.
00214   ///
00215   virtual void EmitConstantPool();
00216 
00217   /// Print assembly representations of the jump tables used by the current
00218   /// function to the current output stream.
00219   ///
00220   void EmitJumpTableInfo();
00221 
00222   /// Emit the specified global variable to the .s file.
00223   virtual void EmitGlobalVariable(const GlobalVariable *GV);
00224 
00225   /// Check to see if the specified global is a special global used by LLVM. If
00226   /// so, emit it and return true, otherwise do nothing and return false.
00227   bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
00228 
00229   /// Emit an alignment directive to the specified power of two boundary. For
00230   /// example, if you pass in 3 here, you will get an 8 byte alignment. If a
00231   /// global value is specified, and if that global has an explicit alignment
00232   /// requested, it will override the alignment request if required for
00233   /// correctness.
00234   ///
00235   void EmitAlignment(unsigned NumBits, const GlobalObject *GO = nullptr) const;
00236 
00237   /// This method prints the label for the specified MachineBasicBlock, an
00238   /// alignment (if present) and a comment describing it if appropriate.
00239   void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
00240 
00241   /// \brief Print a general LLVM constant to the .s file.
00242   void EmitGlobalConstant(const Constant *CV);
00243 
00244   //===------------------------------------------------------------------===//
00245   // Overridable Hooks
00246   //===------------------------------------------------------------------===//
00247 
00248   // Targets can, or in the case of EmitInstruction, must implement these to
00249   // customize output.
00250 
00251   /// This virtual method can be overridden by targets that want to emit
00252   /// something at the start of their file.
00253   virtual void EmitStartOfAsmFile(Module &) {}
00254 
00255   /// This virtual method can be overridden by targets that want to emit
00256   /// something at the end of their file.
00257   virtual void EmitEndOfAsmFile(Module &) {}
00258 
00259   /// Targets can override this to emit stuff before the first basic block in
00260   /// the function.
00261   virtual void EmitFunctionBodyStart() {}
00262 
00263   /// Targets can override this to emit stuff after the last basic block in the
00264   /// function.
00265   virtual void EmitFunctionBodyEnd() {}
00266 
00267   /// Targets can override this to emit stuff at the end of a basic block.
00268   virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB) {}
00269 
00270   /// Targets should implement this to emit instructions.
00271   virtual void EmitInstruction(const MachineInstr *) {
00272     llvm_unreachable("EmitInstruction not implemented");
00273   }
00274 
00275   /// Return the symbol for the specified constant pool entry.
00276   virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
00277 
00278   virtual void EmitFunctionEntryLabel();
00279 
00280   virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
00281 
00282   /// Targets can override this to change how global constants that are part of
00283   /// a C++ static/global constructor list are emitted.
00284   virtual void EmitXXStructor(const Constant *CV) { EmitGlobalConstant(CV); }
00285 
00286   /// Return true if the basic block has exactly one predecessor and the control
00287   /// transfer mechanism between the predecessor and this block is a
00288   /// fall-through.
00289   virtual bool
00290   isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
00291 
00292   /// Targets can override this to customize the output of IMPLICIT_DEF
00293   /// instructions in verbose mode.
00294   virtual void emitImplicitDef(const MachineInstr *MI) const;
00295 
00296   //===------------------------------------------------------------------===//
00297   // Symbol Lowering Routines.
00298   //===------------------------------------------------------------------===//
00299 public:
00300   /// Return the MCSymbol corresponding to the assembler temporary label with
00301   /// the specified stem and unique ID.
00302   MCSymbol *GetTempSymbol(Twine Name, unsigned ID) const;
00303 
00304   /// Return an assembler temporary label with the specified stem.
00305   MCSymbol *GetTempSymbol(Twine Name) const;
00306 
00307   /// Return the MCSymbol for a private symbol with global value name as its
00308   /// base, with the specified suffix.
00309   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
00310                                          StringRef Suffix) const;
00311 
00312   /// Return the MCSymbol for the specified ExternalSymbol.
00313   MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
00314 
00315   /// Return the symbol for the specified jump table entry.
00316   MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
00317 
00318   /// Return the symbol for the specified jump table .set
00319   /// FIXME: privatize to AsmPrinter.
00320   MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
00321 
00322   /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
00323   /// basic block.
00324   MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
00325   MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
00326 
00327   //===------------------------------------------------------------------===//
00328   // Emission Helper Routines.
00329   //===------------------------------------------------------------------===//
00330 public:
00331   /// This is just convenient handler for printing offsets.
00332   void printOffset(int64_t Offset, raw_ostream &OS) const;
00333 
00334   /// Emit a byte directive and value.
00335   ///
00336   void EmitInt8(int Value) const;
00337 
00338   /// Emit a short directive and value.
00339   ///
00340   void EmitInt16(int Value) const;
00341 
00342   /// Emit a long directive and value.
00343   ///
00344   void EmitInt32(int Value) const;
00345 
00346   /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
00347   /// is specified by Size and Hi/Lo specify the labels.  This implicitly uses
00348   /// .set if it is available.
00349   void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
00350                            unsigned Size) const;
00351 
00352   /// Emit something like ".long Hi+Offset-Lo" where the size in bytes of the
00353   /// directive is specified by Size and Hi/Lo specify the labels.  This
00354   /// implicitly uses .set if it is available.
00355   void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
00356                                  const MCSymbol *Lo, unsigned Size) const;
00357 
00358   /// Emit something like ".long Label+Offset" where the size in bytes of the
00359   /// directive is specified by Size and Label specifies the label.  This
00360   /// implicitly uses .set if it is available.
00361   void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
00362                            unsigned Size, bool IsSectionRelative = false) const;
00363 
00364   /// Emit something like ".long Label" where the size in bytes of the directive
00365   /// is specified by Size and Label specifies the label.
00366   void EmitLabelReference(const MCSymbol *Label, unsigned Size,
00367                           bool IsSectionRelative = false) const {
00368     EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
00369   }
00370 
00371   //===------------------------------------------------------------------===//
00372   // Dwarf Emission Helper Routines
00373   //===------------------------------------------------------------------===//
00374 
00375   /// Emit the specified signed leb128 value.
00376   void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
00377 
00378   /// Emit the specified unsigned leb128 value.
00379   void EmitULEB128(uint64_t Value, const char *Desc = nullptr,
00380                    unsigned PadTo = 0) const;
00381 
00382   /// Emit a .byte 42 directive for a DW_CFA_xxx value.
00383   void EmitCFAByte(unsigned Val) const;
00384 
00385   /// Emit a .byte 42 directive that corresponds to an encoding.  If verbose
00386   /// assembly output is enabled, we output comments describing the encoding.
00387   /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
00388   void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
00389 
00390   /// Return the size of the encoding in bytes.
00391   unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
00392 
00393   /// Emit reference to a ttype global with a specified encoding.
00394   void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
00395 
00396   /// Emit the 4-byte offset of Label from the start of its section.  This can
00397   /// be done with a special directive if the target supports it (e.g. cygwin)
00398   /// or by emitting it as an offset from a label at the start of the section.
00399   ///
00400   /// SectionLabel is a temporary label emitted at the start of the section
00401   /// that Label lives in.
00402   void EmitSectionOffset(const MCSymbol *Label,
00403                          const MCSymbol *SectionLabel) const;
00404 
00405   /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
00406   virtual unsigned getISAEncoding() { return 0; }
00407 
00408   /// Emit a dwarf register operation for describing
00409   /// - a small value occupying only part of a register or
00410   /// - a register representing only part of a value.
00411   void EmitDwarfOpPiece(ByteStreamer &Streamer, unsigned SizeInBits,
00412                         unsigned OffsetInBits = 0) const;
00413 
00414 
00415   /// \brief Emit a partial DWARF register operation.
00416   /// \param MLoc             the register
00417   /// \param PieceSize        size and
00418   /// \param PieceOffset      offset of the piece in bits, if this is one
00419   ///                         piece of an aggregate value.
00420   ///
00421   /// If size and offset is zero an operation for the entire
00422   /// register is emitted: Some targets do not provide a DWARF
00423   /// register number for every register.  If this is the case, this
00424   /// function will attempt to emit a DWARF register by emitting a
00425   /// piece of a super-register or by piecing together multiple
00426   /// subregisters that alias the register.
00427   void EmitDwarfRegOpPiece(ByteStreamer &BS, const MachineLocation &MLoc,
00428                            unsigned PieceSize = 0,
00429                            unsigned PieceOffset = 0) const;
00430 
00431   /// EmitDwarfRegOp - Emit a dwarf register operation.
00432   /// \param Indirect   whether this is a register-indirect address
00433   virtual void EmitDwarfRegOp(ByteStreamer &BS, const MachineLocation &MLoc,
00434                               bool Indirect) const;
00435 
00436   //===------------------------------------------------------------------===//
00437   // Dwarf Lowering Routines
00438   //===------------------------------------------------------------------===//
00439 
00440   /// \brief Emit frame instruction to describe the layout of the frame.
00441   void emitCFIInstruction(const MCCFIInstruction &Inst) const;
00442 
00443   //===------------------------------------------------------------------===//
00444   // Inline Asm Support
00445   //===------------------------------------------------------------------===//
00446 public:
00447   // These are hooks that targets can override to implement inline asm
00448   // support.  These should probably be moved out of AsmPrinter someday.
00449 
00450   /// Print information related to the specified machine instr that is
00451   /// independent of the operand, and may be independent of the instr itself.
00452   /// This can be useful for portably encoding the comment character or other
00453   /// bits of target-specific knowledge into the asmstrings.  The syntax used is
00454   /// ${:comment}.  Targets can override this to add support for their own
00455   /// strange codes.
00456   virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
00457                             const char *Code) const;
00458 
00459   /// Print the specified operand of MI, an INLINEASM instruction, using the
00460   /// specified assembler variant.  Targets should override this to format as
00461   /// appropriate.  This method can return true if the operand is erroneous.
00462   virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00463                                unsigned AsmVariant, const char *ExtraCode,
00464                                raw_ostream &OS);
00465 
00466   /// Print the specified operand of MI, an INLINEASM instruction, using the
00467   /// specified assembler variant as an address. Targets should override this to
00468   /// format as appropriate.  This method can return true if the operand is
00469   /// erroneous.
00470   virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
00471                                      unsigned AsmVariant, const char *ExtraCode,
00472                                      raw_ostream &OS);
00473 
00474   /// Let the target do anything it needs to do after emitting inlineasm.
00475   /// This callback can be used restore the original mode in case the
00476   /// inlineasm contains directives to switch modes.
00477   /// \p StartInfo - the original subtarget info before inline asm
00478   /// \p EndInfo   - the final subtarget info after parsing the inline asm,
00479   ///                or NULL if the value is unknown.
00480   virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
00481                                 const MCSubtargetInfo *EndInfo) const;
00482 
00483 private:
00484   /// Private state for PrintSpecial()
00485   // Assign a unique ID to this machine instruction.
00486   mutable const MachineInstr *LastMI;
00487   mutable unsigned LastFn;
00488   mutable unsigned Counter;
00489   mutable unsigned SetCounter;
00490 
00491   /// Emit a blob of inline asm to the output streamer.
00492   void
00493   EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = nullptr,
00494                 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
00495 
00496   /// This method formats and emits the specified machine instruction that is an
00497   /// inline asm.
00498   void EmitInlineAsm(const MachineInstr *MI) const;
00499 
00500   //===------------------------------------------------------------------===//
00501   // Internal Implementation Details
00502   //===------------------------------------------------------------------===//
00503 
00504   /// This emits visibility information about symbol, if this is suported by the
00505   /// target.
00506   void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
00507                       bool IsDefinition = true) const;
00508 
00509   void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
00510 
00511   void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
00512                           const MachineBasicBlock *MBB, unsigned uid) const;
00513   void EmitLLVMUsedList(const ConstantArray *InitList);
00514   /// Emit llvm.ident metadata in an '.ident' directive.
00515   void EmitModuleIdents(Module &M);
00516   void EmitXXStructorList(const Constant *List, bool isCtor);
00517   GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &C);
00518 };
00519 }
00520 
00521 #endif