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 Mangler;
00025   class GlobalVariable;
00026 
00027   class AsmPrinter : public MachineFunctionPass {
00028     /// CurrentSection - The current section we are emitting to.  This is
00029     /// controlled and used by the SwitchSection method.
00030     std::string CurrentSection;
00031     
00032     /// FunctionNumber - This provides a unique ID for each function emitted in
00033     /// this translation unit.  It is autoincremented by SetupMachineFunction,
00034     /// and can be accessed with getFunctionNumber() and 
00035     /// IncrementFunctionNumber().
00036     ///
00037     unsigned FunctionNumber;
00038 
00039   public:
00040     /// Output stream on which we're printing assembly code.
00041     ///
00042     std::ostream &O;
00043 
00044     /// Target machine description.
00045     ///
00046     TargetMachine &TM;
00047 
00048     /// Name-mangler for global names.
00049     ///
00050     Mangler *Mang;
00051 
00052     /// Cache of mangled name for current function. This is recalculated at the
00053     /// beginning of each call to runOnMachineFunction().
00054     ///
00055     std::string CurrentFnName;
00056 
00057     //===------------------------------------------------------------------===//
00058     // Properties to be set by the derived class ctor, used to configure the
00059     // asmwriter.
00060 
00061     /// CommentString - This indicates the comment character used by the
00062     /// assembler.
00063     const char *CommentString;     // Defaults to "#"
00064 
00065     /// GlobalPrefix - If this is set to a non-empty string, it is prepended
00066     /// onto all global symbols.  This is often used for "_" or ".".
00067     const char *GlobalPrefix;    // Defaults to ""
00068 
00069     /// PrivateGlobalPrefix - This prefix is used for globals like constant
00070     /// pool entries that are completely private to the .o file and should not
00071     /// have names in the .o file.  This is often "." or "L".
00072     const char *PrivateGlobalPrefix;   // Defaults to "."
00073     
00074     /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
00075     /// will enclose any GlobalVariable (that isn't a function)
00076     ///
00077     const char *GlobalVarAddrPrefix;       // Defaults to ""
00078     const char *GlobalVarAddrSuffix;       // Defaults to ""
00079 
00080     /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
00081     /// will enclose any GlobalVariable that points to a function.
00082     /// For example, this is used by the IA64 backend to materialize
00083     /// function descriptors, by decorating the ".data8" object with the
00084     /// @fptr( ) link-relocation operator.
00085     ///
00086     const char *FunctionAddrPrefix;       // Defaults to ""
00087     const char *FunctionAddrSuffix;       // Defaults to ""
00088 
00089     /// InlineAsmStart/End - If these are nonempty, they contain a directive to
00090     /// emit before and after an inline assmebly statement.
00091     const char *InlineAsmStart;           // Defaults to "#APP\n"
00092     const char *InlineAsmEnd;             // Defaults to "#NO_APP\n"
00093     
00094     //===--- Data Emission Directives -------------------------------------===//
00095 
00096     /// ZeroDirective - this should be set to the directive used to get some
00097     /// number of zero bytes emitted to the current section.  Common cases are
00098     /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
00099     /// Data*bitsDirective's will be used to emit zero bytes.
00100     const char *ZeroDirective;   // Defaults to "\t.zero\t"
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     /// SwitchToSectionDirective - This is the directive used when we want to
00135     /// emit a global to an arbitrary section.  The section name is emited after
00136     /// this.
00137     const char *SwitchToSectionDirective;  // Defaults to "\t.section\t"
00138     
00139     /// ConstantPoolSection - This is the section that we SwitchToSection right
00140     /// before emitting the constant pool for a function.
00141     const char *ConstantPoolSection;     // Defaults to "\t.section .rodata\n"
00142 
00143     /// StaticCtorsSection - This is the directive that is emitted to switch to
00144     /// a section to emit the static constructor list.
00145     /// Defaults to "\t.section .ctors,\"aw\",@progbits".
00146     const char *StaticCtorsSection;
00147 
00148     /// StaticDtorsSection - This is the directive that is emitted to switch to
00149     /// a section to emit the static destructor list.
00150     /// Defaults to "\t.section .dtors,\"aw\",@progbits".
00151     const char *StaticDtorsSection;
00152     
00153     //===--- Global Variable Emission Directives --------------------------===//
00154     
00155     /// LCOMMDirective - This is the name of a directive (if supported) that can
00156     /// be used to efficiently declare a local (internal) block of zero
00157     /// initialized data in the .bss/.data section.  The syntax expected is:
00158     ///    <LCOMMDirective> SYMBOLNAME LENGTHINBYTES, ALIGNMENT
00159     const char *LCOMMDirective;          // Defaults to null.
00160     
00161     const char *COMMDirective;           // Defaults to "\t.comm\t".
00162     
00163     /// COMMDirectiveTakesAlignment - True if COMMDirective take a third
00164     /// argument that specifies the alignment of the declaration.
00165     bool COMMDirectiveTakesAlignment;    // Defaults to true.
00166     
00167     /// HasDotTypeDotSizeDirective - True if the target has .type and .size
00168     /// directives, this is true for most ELF targets.
00169     bool HasDotTypeDotSizeDirective;     // Defaults to true.
00170   
00171   protected:
00172     AsmPrinter(std::ostream &o, TargetMachine &TM);
00173     
00174   public:
00175     /// SwitchSection - Switch to the specified section of the executable if we
00176     /// are not already in it!  If GV is non-null and if the global has an
00177     /// explicitly requested section, we switch to the section indicated for the
00178     /// global instead of NewSection.
00179     ///
00180     /// If the new section is an empty string, this method forgets what the
00181     /// current section is, but does not emit a .section directive.
00182     ///
00183     void SwitchSection(const char *NewSection, const GlobalValue *GV);
00184 
00185     /// getPreferredAlignmentLog - Return the preferred alignment of the
00186     /// specified global, returned in log form.  This includes an explicitly
00187     /// requested alignment (if the global has one).
00188     unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
00189   protected:
00190     /// doInitialization - Set up the AsmPrinter when we are working on a new
00191     /// module.  If your pass overrides this, it must make sure to explicitly
00192     /// call this implementation.
00193     bool doInitialization(Module &M);
00194 
00195     /// doFinalization - Shut down the asmprinter.  If you override this in your
00196     /// pass, you must make sure to call it explicitly.
00197     bool doFinalization(Module &M);
00198 
00199     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
00200     /// instruction, using the specified assembler variant.  Targets should
00201     /// override this to format as appropriate.  This method can return true if
00202     /// the operand is erroneous.
00203     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
00204                                  unsigned AsmVariant, const char *ExtraCode);
00205     
00206     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
00207     /// instruction, using the specified assembler variant as an address.
00208     /// Targets should override this to format as appropriate.  This method can
00209     /// return true if the operand is erroneous.
00210     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
00211                                        unsigned AsmVariant, 
00212                                        const char *ExtraCode);
00213     
00214     /// SetupMachineFunction - This should be called when a new MachineFunction
00215     /// is being processed from runOnMachineFunction.
00216     void SetupMachineFunction(MachineFunction &MF);
00217     
00218     /// getFunctionNumber - Return a unique ID for the current function.
00219     ///
00220     unsigned getFunctionNumber() const { return FunctionNumber; }
00221     
00222     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
00223     /// not normally call this, as the counter is automatically bumped by
00224     /// SetupMachineFunction.
00225     void IncrementFunctionNumber() { FunctionNumber++; }
00226     
00227     /// EmitConstantPool - Print to the current output stream assembly
00228     /// representations of the constants in the constant pool MCP. This is
00229     /// used to print out constants which have been "spilled to memory" by
00230     /// the code generator.
00231     ///
00232     void EmitConstantPool(MachineConstantPool *MCP);
00233 
00234     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
00235     /// special global used by LLVM.  If so, emit it and return true, otherwise
00236     /// do nothing and return false.
00237     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
00238 
00239     /// EmitAlignment - Emit an alignment directive to the specified power of
00240     /// two boundary.  For example, if you pass in 3 here, you will get an 8
00241     /// byte alignment.  If a global value is specified, and if that global has
00242     /// an explicit alignment requested, it will override the alignment request.
00243     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
00244 
00245     /// EmitZeros - Emit a block of zeros.
00246     ///
00247     void EmitZeros(uint64_t NumZeros) const;
00248 
00249     /// EmitConstantValueOnly - Print out the specified constant, without a
00250     /// storage class.  Only constants of first-class type are allowed here.
00251     void EmitConstantValueOnly(const Constant *CV);
00252 
00253     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
00254     ///
00255     void EmitGlobalConstant(const Constant* CV);
00256     
00257     /// printInlineAsm - This method formats and prints the specified machine
00258     /// instruction that is an inline asm.
00259     void printInlineAsm(const MachineInstr *MI) const;
00260   private:
00261     void EmitXXStructorList(Constant *List);
00262 
00263   };
00264 }
00265 
00266 #endif