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/Support/DataTypes.h"
00021 #include "llvm/ADT/DenseMap.h"
00022 #include <set>
00023 
00024 namespace llvm {
00025   class GCStrategy;
00026   class Constant;
00027   class ConstantArray;
00028   class ConstantInt;
00029   class ConstantStruct;
00030   class ConstantVector;
00031   class GCMetadataPrinter;
00032   class GlobalVariable;
00033   class MachineConstantPoolEntry;
00034   class MachineConstantPoolValue;
00035   class DwarfWriter;
00036   class Mangler;
00037   class Section;
00038   class TargetAsmInfo;
00039   class Type;
00040   class raw_ostream;
00041 
00042   /// AsmPrinter - This class is intended to be used as a driving class for all
00043   /// asm writers.
00044   class AsmPrinter : public MachineFunctionPass {
00045     static char ID;
00046 
00047     /// FunctionNumber - This provides a unique ID for each function emitted in
00048     /// this translation unit.  It is autoincremented by SetupMachineFunction,
00049     /// and can be accessed with getFunctionNumber() and 
00050     /// IncrementFunctionNumber().
00051     ///
00052     unsigned FunctionNumber;
00053 
00054     /// DW -This is needed because printDeclare() has to insert
00055     /// DbgVariable entries into the dwarf table. This is a short term hack
00056     /// that ought be fixed soon.
00057     DwarfWriter *DW;
00058     
00059     // GCMetadataPrinters - The garbage collection metadata printer table.
00060     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
00061     typedef gcp_map_type::iterator gcp_iterator;
00062     gcp_map_type GCMetadataPrinters;
00063     
00064   protected:
00065     // Necessary for external weak linkage support
00066     std::set<const GlobalValue*> ExtWeakSymbols;
00067 
00068   public:
00069     /// Output stream on which we're printing assembly code.
00070     ///
00071     raw_ostream &O;
00072 
00073     /// Target machine description.
00074     ///
00075     TargetMachine &TM;
00076     
00077     /// Target Asm Printer information.
00078     ///
00079     const TargetAsmInfo *TAI;
00080 
00081     /// Target Register Information.
00082     ///
00083     const TargetRegisterInfo *TRI;
00084 
00085     /// Name-mangler for global names.
00086     ///
00087     Mangler *Mang;
00088 
00089     /// Cache of mangled name for current function. This is recalculated at the
00090     /// beginning of each call to runOnMachineFunction().
00091     ///
00092     std::string CurrentFnName;
00093     
00094     /// CurrentSection - The current section we are emitting to.  This is
00095     /// controlled and used by the SwitchSection method.
00096     std::string CurrentSection;
00097     const Section* CurrentSection_;
00098 
00099     /// IsInTextSection - True if the current section we are emitting to is a
00100     /// text section.
00101     bool IsInTextSection;
00102   
00103   protected:
00104     AsmPrinter(raw_ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
00105     
00106   public:
00107     virtual ~AsmPrinter();
00108     
00109     /// SwitchToTextSection - Switch to the specified section of the executable
00110     /// if we are not already in it!  If GV is non-null and if the global has an
00111     /// explicitly requested section, we switch to the section indicated for the
00112     /// global instead of NewSection.
00113     ///
00114     /// If the new section is an empty string, this method forgets what the
00115     /// current section is, but does not emit a .section directive.
00116     ///
00117     /// This method is used when about to emit executable code.
00118     ///
00119     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
00120 
00121     /// SwitchToDataSection - Switch to the specified section of the executable
00122     /// if we are not already in it!  If GV is non-null and if the global has an
00123     /// explicitly requested section, we switch to the section indicated for the
00124     /// global instead of NewSection.
00125     ///
00126     /// If the new section is an empty string, this method forgets what the
00127     /// current section is, but does not emit a .section directive.
00128     ///
00129     /// This method is used when about to emit data.  For most assemblers, this
00130     /// is the same as the SwitchToTextSection method, but not all assemblers
00131     /// are the same.
00132     ///
00133     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
00134 
00135     /// SwitchToSection - Switch to the specified section of the executable if
00136     /// we are not already in it!
00137     void SwitchToSection(const Section* NS);
00138 
00139     /// getGlobalLinkName - Returns the asm/link name of of the specified
00140     /// global variable.  Should be overridden by each target asm printer to
00141     /// generate the appropriate value.
00142     virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
00143 
00144     /// EmitExternalGlobal - Emit the external reference to a global variable.
00145     /// Should be overridden if an indirect reference should be used.
00146     virtual void EmitExternalGlobal(const GlobalVariable *GV);
00147 
00148     /// getCurrentFunctionEHName - Called to return (and cache) the
00149     /// CurrentFnEHName.
00150     /// 
00151     std::string getCurrentFunctionEHName(const MachineFunction *MF);
00152 
00153   protected:
00154     /// getAnalysisUsage - Record analysis usage.
00155     /// 
00156     void getAnalysisUsage(AnalysisUsage &AU) const;
00157     
00158     /// doInitialization - Set up the AsmPrinter when we are working on a new
00159     /// module.  If your pass overrides this, it must make sure to explicitly
00160     /// call this implementation.
00161     bool doInitialization(Module &M);
00162 
00163     /// doFinalization - Shut down the asmprinter.  If you override this in your
00164     /// pass, you must make sure to call it explicitly.
00165     bool doFinalization(Module &M);
00166     
00167     /// PrintSpecial - Print information related to the specified machine instr
00168     /// that is independent of the operand, and may be independent of the instr
00169     /// itself.  This can be useful for portably encoding the comment character
00170     /// or other bits of target-specific knowledge into the asmstrings.  The
00171     /// syntax used is ${:comment}.  Targets can override this to add support
00172     /// for their own strange codes.
00173     virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
00174 
00175     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
00176     /// instruction, using the specified assembler variant.  Targets should
00177     /// override this to format as appropriate.  This method can return true if
00178     /// the operand is erroneous.
00179     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00180                                  unsigned AsmVariant, const char *ExtraCode);
00181     
00182     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
00183     /// instruction, using the specified assembler variant as an address.
00184     /// Targets should override this to format as appropriate.  This method can
00185     /// return true if the operand is erroneous.
00186     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
00187                                        unsigned AsmVariant, 
00188                                        const char *ExtraCode);
00189     
00190     /// SetupMachineFunction - This should be called when a new MachineFunction
00191     /// is being processed from runOnMachineFunction.
00192     void SetupMachineFunction(MachineFunction &MF);
00193     
00194     /// getFunctionNumber - Return a unique ID for the current function.
00195     ///
00196     unsigned getFunctionNumber() const { return FunctionNumber; }
00197     
00198     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
00199     /// not normally call this, as the counter is automatically bumped by
00200     /// SetupMachineFunction.
00201     void IncrementFunctionNumber() { FunctionNumber++; }
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     void EmitConstantPool(MachineConstantPool *MCP);
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(MachineJumpTableInfo *MJTI, MachineFunction &MF);
00214     
00215     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
00216     /// special global used by LLVM.  If so, emit it and return true, otherwise
00217     /// do nothing and return false.
00218     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
00219     
00220   public:
00221     //===------------------------------------------------------------------===//
00222     /// LEB 128 number encoding.
00223 
00224     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
00225     /// representing an unsigned leb128 value.
00226     void PrintULEB128(unsigned Value) const;
00227 
00228     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
00229     /// representing a signed leb128 value.
00230     void PrintSLEB128(int Value) const;
00231 
00232     //===------------------------------------------------------------------===//
00233     // Emission and print routines
00234     //
00235 
00236     /// PrintHex - Print a value as a hexidecimal value.
00237     ///
00238     void PrintHex(int Value) const;
00239 
00240     /// EOL - Print a newline character to asm stream.  If a comment is present
00241     /// then it will be printed first.  Comments should not contain '\n'.
00242     void EOL() const;
00243     void EOL(const std::string &Comment) const;
00244     void EOL(const char* Comment) const;
00245     
00246     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
00247     /// unsigned leb128 value.
00248     void EmitULEB128Bytes(unsigned Value) const;
00249     
00250     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
00251     /// signed leb128 value.
00252     void EmitSLEB128Bytes(int Value) const;
00253     
00254     /// EmitInt8 - Emit a byte directive and value.
00255     ///
00256     void EmitInt8(int Value) const;
00257 
00258     /// EmitInt16 - Emit a short directive and value.
00259     ///
00260     void EmitInt16(int Value) const;
00261 
00262     /// EmitInt32 - Emit a long directive and value.
00263     ///
00264     void EmitInt32(int Value) const;
00265 
00266     /// EmitInt64 - Emit a long long directive and value.
00267     ///
00268     void EmitInt64(uint64_t Value) const;
00269 
00270     /// EmitString - Emit a string with quotes and a null terminator.
00271     /// Special characters are emitted properly.
00272     /// @verbatim (Eg. '\t') @endverbatim
00273     void EmitString(const std::string &String) const;
00274     
00275     /// EmitFile - Emit a .file directive.
00276     void EmitFile(unsigned Number, const std::string &Name) const;
00277 
00278     //===------------------------------------------------------------------===//
00279 
00280     /// EmitAlignment - Emit an alignment directive to the specified power of
00281     /// two boundary.  For example, if you pass in 3 here, you will get an 8
00282     /// byte alignment.  If a global value is specified, and if that global has
00283     /// an explicit alignment requested, it will unconditionally override the
00284     /// alignment request.  However, if ForcedAlignBits is specified, this value
00285     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
00286     /// and the alignment computed with NumBits and the global.  If UseFillExpr
00287     /// is true, it also emits an optional second value FillValue which the
00288     /// assembler uses to fill gaps to match alignment for text sections if the
00289     /// has specified a non-zero fill value.
00290     ///
00291     /// The algorithm is:
00292     ///     Align = NumBits;
00293     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
00294     ///     Align = std::max(Align, ForcedAlignBits);
00295     ///
00296     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
00297                        unsigned ForcedAlignBits = 0,
00298                        bool UseFillExpr = true) const;
00299 
00300     /// printLabel - This method prints a local label used by debug and
00301     /// exception handling tables.
00302     void printLabel(const MachineInstr *MI) const;
00303     void printLabel(unsigned Id) const;
00304 
00305     /// printDeclare - This method prints a local variable declaration used by
00306     /// debug tables.
00307     void printDeclare(const MachineInstr *MI) const;
00308 
00309   protected:
00310     /// EmitZeros - Emit a block of zeros.
00311     ///
00312     void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
00313 
00314     /// EmitString - Emit a zero-byte-terminated string constant.
00315     ///
00316     virtual void EmitString(const ConstantArray *CVA) const;
00317 
00318     /// EmitConstantValueOnly - Print out the specified constant, without a
00319     /// storage class.  Only constants of first-class type are allowed here.
00320     void EmitConstantValueOnly(const Constant *CV);
00321 
00322     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
00323     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
00324 
00325     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
00326     
00327     /// printInlineAsm - This method formats and prints the specified machine
00328     /// instruction that is an inline asm.
00329     void printInlineAsm(const MachineInstr *MI) const;
00330 
00331     /// printImplicitDef - This method prints the specified machine instruction
00332     /// that is an implicit def.
00333     virtual void printImplicitDef(const MachineInstr *MI) const;
00334     
00335     /// printBasicBlockLabel - This method prints the label for the specified
00336     /// MachineBasicBlock
00337     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
00338                                       bool printAlign = false,
00339                                       bool printColon = false,
00340                                       bool printComment = true) const;
00341                                       
00342     /// printPICJumpTableSetLabel - This method prints a set label for the
00343     /// specified MachineBasicBlock for a jumptable entry.
00344     virtual void printPICJumpTableSetLabel(unsigned uid,
00345                                            const MachineBasicBlock *MBB) const;
00346     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
00347                                            const MachineBasicBlock *MBB) const;
00348     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
00349                                         const MachineBasicBlock *MBB,
00350                                         unsigned uid) const;
00351     
00352     /// printDataDirective - This method prints the asm directive for the
00353     /// specified type.
00354     void printDataDirective(const Type *type, unsigned AddrSpace = 0);
00355 
00356     /// printSuffixedName - This prints a name with preceding 
00357     /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
00358     /// correctly.
00359     void printSuffixedName(const char *Name, const char *Suffix,
00360                            const char *Prefix = 0);
00361     void printSuffixedName(const std::string &Name, const char* Suffix);
00362 
00363     /// printVisibility - This prints visibility information about symbol, if
00364     /// this is suported by the target.
00365     void printVisibility(const std::string& Name, unsigned Visibility) const;
00366 
00367     /// printOffset - This is just convenient handler for printing offsets.
00368     void printOffset(int64_t Offset) const;
00369 
00370   private:
00371     const GlobalValue *findGlobalValue(const Constant* CV);
00372     void EmitLLVMUsedList(Constant *List);
00373     void EmitXXStructorList(Constant *List);
00374     void EmitGlobalConstantStruct(const ConstantStruct* CVS,
00375                                   unsigned AddrSpace);
00376     void EmitGlobalConstantArray(const ConstantArray* CVA);
00377     void EmitGlobalConstantVector(const ConstantVector* CP);
00378     void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
00379     void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
00380     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
00381   };
00382 }
00383 
00384 #endif