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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class is intended to be used as a base class for target-specific
00011 // asmwriters.  This class primarily takes care of printing global constants,
00012 // which are printed in a very similar way across all targets.
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 
00022 namespace llvm {
00023   class Constant;
00024   class ConstantArray;
00025   class Mangler;
00026   class GlobalVariable;
00027   class MachineConstantPoolEntry;
00028 
00029   class AsmPrinter : public MachineFunctionPass {
00030     /// FunctionNumber - This provides a unique ID for each function emitted in
00031     /// this translation unit.  It is autoincremented by SetupMachineFunction,
00032     /// and can be accessed with getFunctionNumber() and 
00033     /// IncrementFunctionNumber().
00034     ///
00035     unsigned FunctionNumber;
00036 
00037   public:
00038     /// Output stream on which we're printing assembly code.
00039     ///
00040     std::ostream &O;
00041 
00042     /// Target machine description.
00043     ///
00044     TargetMachine &TM;
00045 
00046     /// Name-mangler for global names.
00047     ///
00048     Mangler *Mang;
00049 
00050     /// Cache of mangled name for current function. This is recalculated at the
00051     /// beginning of each call to runOnMachineFunction().
00052     ///
00053     std::string CurrentFnName;
00054 
00055     //===------------------------------------------------------------------===//
00056     // Properties to be set by the derived class ctor, used to configure the
00057     // asmwriter.
00058 
00059     /// CommentString - This indicates the comment character used by the
00060     /// assembler.
00061     const char *CommentString;     // Defaults to "#"
00062 
00063     /// GlobalPrefix - If this is set to a non-empty string, it is prepended
00064     /// onto all global symbols.  This is often used for "_" or ".".
00065     const char *GlobalPrefix;    // Defaults to ""
00066 
00067     /// PrivateGlobalPrefix - This prefix is used for globals like constant
00068     /// pool entries that are completely private to the .o file and should not
00069     /// have names in the .o file.  This is often "." or "L".
00070     const char *PrivateGlobalPrefix;   // Defaults to "."
00071     
00072     /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
00073     /// will enclose any GlobalVariable (that isn't a function)
00074     ///
00075     const char *GlobalVarAddrPrefix;       // Defaults to ""
00076     const char *GlobalVarAddrSuffix;       // Defaults to ""
00077 
00078     /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
00079     /// will enclose any GlobalVariable that points to a function.
00080     /// For example, this is used by the IA64 backend to materialize
00081     /// function descriptors, by decorating the ".data8" object with the
00082     /// \literal @fptr( ) \endliteral
00083     /// link-relocation operator.
00084     ///
00085     const char *FunctionAddrPrefix;       // Defaults to ""
00086     const char *FunctionAddrSuffix;       // Defaults to ""
00087 
00088     /// InlineAsmStart/End - If these are nonempty, they contain a directive to
00089     /// emit before and after an inline assmebly statement.
00090     const char *InlineAsmStart;           // Defaults to "#APP\n"
00091     const char *InlineAsmEnd;             // Defaults to "#NO_APP\n"
00092     
00093     //===--- Data Emission Directives -------------------------------------===//
00094 
00095     /// ZeroDirective - this should be set to the directive used to get some
00096     /// number of zero bytes emitted to the current section.  Common cases are
00097     /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
00098     /// Data*bitsDirective's will be used to emit zero bytes.
00099     const char *ZeroDirective;   // Defaults to "\t.zero\t"
00100     const char *ZeroDirectiveSuffix;  // Defaults to ""
00101 
00102     /// AsciiDirective - This directive allows emission of an ascii string with
00103     /// the standard C escape characters embedded into it.
00104     const char *AsciiDirective;  // Defaults to "\t.ascii\t"
00105     
00106     /// AscizDirective - If not null, this allows for special handling of
00107     /// zero terminated strings on this target.  This is commonly supported as
00108     /// ".asciz".  If a target doesn't support this, it can be set to null.
00109     const char *AscizDirective;  // Defaults to "\t.asciz\t"
00110 
00111     /// DataDirectives - These directives are used to output some unit of
00112     /// integer data to the current section.  If a data directive is set to
00113     /// null, smaller data directives will be used to emit the large sizes.
00114     const char *Data8bitsDirective;   // Defaults to "\t.byte\t"
00115     const char *Data16bitsDirective;  // Defaults to "\t.short\t"
00116     const char *Data32bitsDirective;  // Defaults to "\t.long\t"
00117     const char *Data64bitsDirective;  // Defaults to "\t.quad\t"
00118 
00119     //===--- Alignment Information ----------------------------------------===//
00120 
00121     /// AlignDirective - The directive used to emit round up to an alignment
00122     /// boundary.
00123     ///
00124     const char *AlignDirective;       // Defaults to "\t.align\t"
00125 
00126     /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
00127     /// emits ".align N" directives, where N is the number of bytes to align to.
00128     /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
00129     /// boundary.
00130     bool AlignmentIsInBytes;          // Defaults to true
00131     
00132     //===--- Section Switching Directives ---------------------------------===//
00133     
00134     /// CurrentSection - The current section we are emitting to.  This is
00135     /// controlled and used by the SwitchSection method.
00136     std::string CurrentSection;
00137     
00138     /// SwitchToSectionDirective - This is the directive used when we want to
00139     /// emit a global to an arbitrary section.  The section name is emited after
00140     /// this.
00141     const char *SwitchToSectionDirective;  // Defaults to "\t.section\t"
00142     
00143     /// TextSectionStartSuffix - This is printed after each start of section
00144     /// directive for text sections.
00145     const char *TextSectionStartSuffix;        // Defaults to "".
00146 
00147     /// DataSectionStartSuffix - This is printed after each start of section
00148     /// directive for data sections.
00149     const char *DataSectionStartSuffix;        // Defaults to "".
00150     
00151     /// SectionEndDirectiveSuffix - If non-null, the asm printer will close each
00152     /// section with the section name and this suffix printed.
00153     const char *SectionEndDirectiveSuffix; // Defaults to null.
00154     
00155     /// ConstantPoolSection - This is the section that we SwitchToSection right
00156     /// before emitting the constant pool for a function.
00157     const char *ConstantPoolSection;     // Defaults to "\t.section .rodata\n"
00158 
00159     /// JumpTableDataSection - This is the section that we SwitchToSection right
00160     /// before emitting the jump tables for a function when the relocation model
00161     /// is not PIC.
00162     const char *JumpTableDataSection;     // Defaults to "\t.section .rodata\n"
00163     
00164     /// JumpTableTextSection - This is the section that we SwitchToSection right
00165     /// before emitting the jump tables for a function when the relocation model
00166     /// is PIC.
00167     const char *JumpTableTextSection;     // Defaults to "\t.text\n"
00168     
00169     /// StaticCtorsSection - This is the directive that is emitted to switch to
00170     /// a section to emit the static constructor list.
00171     /// Defaults to "\t.section .ctors,\"aw\",@progbits".
00172     const char *StaticCtorsSection;
00173 
00174     /// StaticDtorsSection - This is the directive that is emitted to switch to
00175     /// a section to emit the static destructor list.
00176     /// Defaults to "\t.section .dtors,\"aw\",@progbits".
00177     const char *StaticDtorsSection;
00178 
00179     /// FourByteConstantSection, EightByteConstantSection,
00180     /// SixteenByteConstantSection - These are special sections where we place
00181     /// 4-, 8-, and 16- byte constant literals.
00182     const char *FourByteConstantSection;
00183     const char *EightByteConstantSection;
00184     const char *SixteenByteConstantSection;
00185     
00186     //===--- Global Variable Emission Directives --------------------------===//
00187     
00188     /// LCOMMDirective - This is the name of a directive (if supported) that can
00189     /// be used to efficiently declare a local (internal) block of zero
00190     /// initialized data in the .bss/.data section.  The syntax expected is:
00191     /// \literal <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
00192     /// \endliteral
00193     const char *LCOMMDirective;          // Defaults to null.
00194     
00195     const char *COMMDirective;           // Defaults to "\t.comm\t".
00196     
00197     /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
00198     /// argument that specifies the alignment of the declaration.
00199     bool COMMDirectiveTakesAlignment;    // Defaults to true.
00200     
00201     /// HasDotTypeDotSizeDirective - True if the target has .type and .size
00202     /// directives, this is true for most ELF targets.
00203     bool HasDotTypeDotSizeDirective;     // Defaults to true.
00204   
00205   protected:
00206     AsmPrinter(std::ostream &o, TargetMachine &TM);
00207     
00208   public:
00209     /// SwitchToTextSection - Switch to the specified section of the executable
00210     /// if we are not already in it!  If GV is non-null and if the global has an
00211     /// explicitly requested section, we switch to the section indicated for the
00212     /// global instead of NewSection.
00213     ///
00214     /// If the new section is an empty string, this method forgets what the
00215     /// current section is, but does not emit a .section directive.
00216     ///
00217     /// This method is used when about to emit executable code.
00218     ///
00219     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
00220 
00221     /// SwitchToDataSection - Switch to the specified section of the executable
00222     /// if we are not already in it!  If GV is non-null and if the global has an
00223     /// explicitly requested section, we switch to the section indicated for the
00224     /// global instead of NewSection.
00225     ///
00226     /// If the new section is an empty string, this method forgets what the
00227     /// current section is, but does not emit a .section directive.
00228     ///
00229     /// This method is used when about to emit data.  For most assemblers, this
00230     /// is the same as the SwitchToTextSection method, but not all assemblers
00231     /// are the same.
00232     ///
00233     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
00234     
00235     /// getPreferredAlignmentLog - Return the preferred alignment of the
00236     /// specified global, returned in log form.  This includes an explicitly
00237     /// requested alignment (if the global has one).
00238     unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
00239   protected:
00240     /// doInitialization - Set up the AsmPrinter when we are working on a new
00241     /// module.  If your pass overrides this, it must make sure to explicitly
00242     /// call this implementation.
00243     bool doInitialization(Module &M);
00244 
00245     /// doFinalization - Shut down the asmprinter.  If you override this in your
00246     /// pass, you must make sure to call it explicitly.
00247     bool doFinalization(Module &M);
00248 
00249     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
00250     /// instruction, using the specified assembler variant.  Targets should
00251     /// override this to format as appropriate.  This method can return true if
00252     /// the operand is erroneous.
00253     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00254                                  unsigned AsmVariant, const char *ExtraCode);
00255     
00256     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
00257     /// instruction, using the specified assembler variant as an address.
00258     /// Targets should override this to format as appropriate.  This method can
00259     /// return true if the operand is erroneous.
00260     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
00261                                        unsigned AsmVariant, 
00262                                        const char *ExtraCode);
00263     
00264     /// SetupMachineFunction - This should be called when a new MachineFunction
00265     /// is being processed from runOnMachineFunction.
00266     void SetupMachineFunction(MachineFunction &MF);
00267     
00268     /// getFunctionNumber - Return a unique ID for the current function.
00269     ///
00270     unsigned getFunctionNumber() const { return FunctionNumber; }
00271     
00272     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
00273     /// not normally call this, as the counter is automatically bumped by
00274     /// SetupMachineFunction.
00275     void IncrementFunctionNumber() { FunctionNumber++; }
00276     
00277     /// EmitConstantPool - Print to the current output stream assembly
00278     /// representations of the constants in the constant pool MCP. This is
00279     /// used to print out constants which have been "spilled to memory" by
00280     /// the code generator.
00281     ///
00282     void EmitConstantPool(MachineConstantPool *MCP);
00283 
00284     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
00285     /// used by the current function to the current output stream.  
00286     ///
00287     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
00288     
00289     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
00290     /// special global used by LLVM.  If so, emit it and return true, otherwise
00291     /// do nothing and return false.
00292     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
00293 
00294     /// EmitAlignment - Emit an alignment directive to the specified power of
00295     /// two boundary.  For example, if you pass in 3 here, you will get an 8
00296     /// byte alignment.  If a global value is specified, and if that global has
00297     /// an explicit alignment requested, it will override the alignment request.
00298     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
00299 
00300     /// EmitZeros - Emit a block of zeros.
00301     ///
00302     void EmitZeros(uint64_t NumZeros) const;
00303 
00304     /// EmitString - Emit a zero-byte-terminated string constant.
00305     ///
00306     virtual void EmitString(const ConstantArray *CVA) const;
00307 
00308     /// EmitConstantValueOnly - Print out the specified constant, without a
00309     /// storage class.  Only constants of first-class type are allowed here.
00310     void EmitConstantValueOnly(const Constant *CV);
00311 
00312     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
00313     ///
00314     void EmitGlobalConstant(const Constant* CV);
00315     
00316     /// printInlineAsm - This method formats and prints the specified machine
00317     /// instruction that is an inline asm.
00318     void printInlineAsm(const MachineInstr *MI) const;
00319     
00320     /// printBasicBlockLabel - This method prints the label for the specified
00321     /// MachineBasicBlock
00322     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
00323                                       bool printColon = false,
00324                                       bool printComment = true) const;
00325     
00326   private:
00327     void EmitXXStructorList(Constant *List);
00328     void EmitConstantPool(unsigned Alignment, const char *Section,
00329                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
00330 
00331   };
00332 }
00333 
00334 #endif