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