LLVM API Documentation

MCStreamer.h

Go to the documentation of this file.
00001 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- 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 declares the MCStreamer class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_MC_MCSTREAMER_H
00015 #define LLVM_MC_MCSTREAMER_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 
00019 namespace llvm {
00020   class AsmPrinter;
00021   class MCContext;
00022   class MCValue;
00023   class MCInst;
00024   class MCSection;
00025   class MCSymbol;
00026   class StringRef;
00027   class raw_ostream;
00028   class TargetAsmInfo;
00029 
00030   /// MCStreamer - Streaming machine code generation interface.  This interface
00031   /// is intended to provide a programatic interface that is very similar to the
00032   /// level that an assembler .s file provides.  It has callbacks to emit bytes,
00033   /// "emit directives", etc.  The implementation of this interface retains
00034   /// state to know what the current section is etc.
00035   ///
00036   /// There are multiple implementations of this interface: one for writing out
00037   /// a .s file, and implementations that write out .o files of various formats.
00038   ///
00039   class MCStreamer {
00040   public:
00041     enum SymbolAttr {
00042       Global,         /// .globl
00043       Hidden,         /// .hidden (ELF)
00044       IndirectSymbol, /// .indirect_symbol (Apple)
00045       Internal,       /// .internal (ELF)
00046       LazyReference,  /// .lazy_reference (Apple)
00047       NoDeadStrip,    /// .no_dead_strip (Apple)
00048       PrivateExtern,  /// .private_extern (Apple)
00049       Protected,      /// .protected (ELF)
00050       Reference,      /// .reference (Apple)
00051       Weak,           /// .weak
00052       WeakDefinition, /// .weak_definition (Apple)
00053       WeakReference,  /// .weak_reference (Apple)
00054 
00055       SymbolAttrFirst = Global,
00056       SymbolAttrLast = WeakReference
00057     };
00058 
00059     enum AssemblerFlag {
00060       SubsectionsViaSymbols  /// .subsections_via_symbols (Apple)
00061     };
00062 
00063   private:
00064     MCContext &Context;
00065 
00066     MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
00067     MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
00068 
00069   protected:
00070     MCStreamer(MCContext &Ctx);
00071 
00072     /// CurSection - This is the current section code is being emitted to, it is
00073     /// kept up to date by SwitchSection.
00074     const MCSection *CurSection;
00075   public:
00076     virtual ~MCStreamer();
00077 
00078     MCContext &getContext() const { return Context; }
00079 
00080     /// @name Symbol & Section Management
00081     /// @{
00082 
00083     /// SwitchSection - Set the current section where code is being emitted to
00084     /// @param Section.  This is required to update CurSection.
00085     ///
00086     /// This corresponds to assembler directives like .section, .text, etc.
00087     virtual void SwitchSection(const MCSection *Section) = 0;
00088 
00089     
00090     /// getCurrentSection - Return the current seciton that the streamer is
00091     /// emitting code to.
00092     const MCSection *getCurrentSection() const { return CurSection; }
00093     
00094     /// EmitLabel - Emit a label for @param Symbol into the current section.
00095     ///
00096     /// This corresponds to an assembler statement such as:
00097     ///   foo:
00098     ///
00099     /// @param Symbol - The symbol to emit. A given symbol should only be
00100     /// emitted as a label once, and symbols emitted as a label should never be
00101     /// used in an assignment.
00102     //
00103     // FIXME: What to do about the current section? Should we get rid of the
00104     // symbol section in the constructor and initialize it here?
00105     virtual void EmitLabel(MCSymbol *Symbol) = 0;
00106 
00107     /// EmitAssemblerFlag - Note in the output the specified @param Flag
00108     virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
00109 
00110     /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
00111     ///
00112     /// This corresponds to an assembler statement such as:
00113     ///  symbol = value
00114     ///
00115     /// The assignment generates no code, but has the side effect of binding the
00116     /// value in the current context. For the assembly streamer, this prints the
00117     /// binding into the .s file.
00118     ///
00119     /// @param Symbol - The symbol being assigned to.
00120     /// @param Value - The value for the symbol.
00121     /// @param MakeAbsolute - If true, then the symbol should be given the
00122     /// absolute value of @param Value, even if @param Value would be
00123     /// relocatable expression. This corresponds to the ".set" directive.
00124     virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
00125                                 bool MakeAbsolute = false) = 0;
00126 
00127     /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
00128     //
00129     // FIXME: This doesn't make much sense, could we just have attributes be on
00130     // the symbol and make the printer smart enough to add the right symbols?
00131     // This should work as long as the order of attributes in the file doesn't
00132     // matter.
00133     virtual void EmitSymbolAttribute(MCSymbol *Symbol,
00134                                      SymbolAttr Attribute) = 0;
00135 
00136     /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
00137     ///
00138     /// @param Symbol - The symbol to have its n_desc field set.
00139     /// @param DescValue - The value to set into the n_desc field.
00140     virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
00141 
00142     /// EmitLocalSymbol - Emit a local symbol of @param Value to @param Symbol.
00143     ///
00144     /// @param Symbol - The local symbol being created.
00145     /// @param Value - The value for the symbol.
00146     virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) = 0;
00147 
00148     /// EmitCommonSymbol - Emit a common or local common symbol of @param Size
00149     /// with the @param Pow2Alignment if non-zero.
00150     ///
00151     /// @param Symbol - The common symbol to emit.
00152     /// @param Size - The size of the common symbol.
00153     /// @param Pow2Alignment - The alignment of the common symbol if non-zero.
00154     /// @param IsLocal - If true, then the symbol is to be a local common
00155     virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
00156                                   unsigned Pow2Alignment, bool IsLocal) = 0;
00157 
00158     /// EmitZerofill - Emit a the zerofill section and possiblity a symbol, if
00159     /// @param Symbol is non-NULL, for @param Size and with the @param
00160     /// Pow2Alignment if non-zero.
00161     ///
00162     /// @param Section - The zerofill section to create and or to put the symbol
00163     /// @param Symbol - The zerofill symbol to emit, if non-NULL.
00164     /// @param Size - The size of the zerofill symbol.
00165     /// @param Pow2Alignment - The alignment of the zerofill symbol if non-zero.
00166     virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = 0,
00167                               unsigned Size = 0,unsigned Pow2Alignment = 0) = 0;
00168 
00169     /// @}
00170     /// @name Generating Data
00171     /// @{
00172 
00173     /// EmitBytes - Emit the bytes in @param Data into the output.
00174     ///
00175     /// This is used to implement assembler directives such as .byte, .ascii,
00176     /// etc.
00177     virtual void EmitBytes(const StringRef &Data) = 0;
00178 
00179     /// EmitValue - Emit the expression @param Value into the output as a native
00180     /// integer of the given @param Size bytes.
00181     ///
00182     /// This is used to implement assembler directives such as .word, .quad,
00183     /// etc.
00184     ///
00185     /// @param Value - The value to emit.
00186     /// @param Size - The size of the integer (in bytes) to emit. This must
00187     /// match a native machine width.
00188     virtual void EmitValue(const MCValue &Value, unsigned Size) = 0;
00189 
00190     /// EmitValueToAlignment - Emit some number of copies of @param Value until
00191     /// the byte alignment @param ByteAlignment is reached.
00192     ///
00193     /// If the number of bytes need to emit for the alignment is not a multiple
00194     /// of @param ValueSize, then the contents of the emitted fill bytes is
00195     /// undefined.
00196     ///
00197     /// This used to implement the .align assembler directive.
00198     ///
00199     /// @param ByteAlignment - The alignment to reach. This must be a power of
00200     /// two on some targets.
00201     /// @param Value - The value to use when filling bytes.
00202     /// @param Size - The size of the integer (in bytes) to emit for @param
00203     /// Value. This must match a native machine width.
00204     /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
00205     /// the alignment cannot be reached in this many bytes, no bytes are
00206     /// emitted.
00207     virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
00208                                       unsigned ValueSize = 1,
00209                                       unsigned MaxBytesToEmit = 0) = 0;
00210 
00211     /// EmitValueToOffset - Emit some number of copies of @param Value until the
00212     /// byte offset @param Offset is reached.
00213     ///
00214     /// This is used to implement assembler directives such as .org.
00215     ///
00216     /// @param Offset - The offset to reach.This may be an expression, but the
00217     /// expression must be associated with the current section.
00218     /// @param Value - The value to use when filling bytes.
00219     // 
00220     // FIXME: How are we going to signal failures out of this?
00221     virtual void EmitValueToOffset(const MCValue &Offset, 
00222                                    unsigned char Value = 0) = 0;
00223     
00224     /// @}
00225 
00226     /// EmitInstruction - Emit the given @param Instruction into the current
00227     /// section.
00228     virtual void EmitInstruction(const MCInst &Inst) = 0;
00229 
00230     /// Finish - Finish emission of machine code and flush any output.
00231     virtual void Finish() = 0;
00232   };
00233 
00234   /// createNullStreamer - Create a dummy machine code streamer, which does
00235   /// nothing. This is useful for timing the assembler front end.
00236   MCStreamer *createNullStreamer(MCContext &Ctx);
00237 
00238   /// createAsmStreamer - Create a machine code streamer which will print out
00239   /// assembly for the native target, suitable for compiling with a native
00240   /// assembler.
00241   ///
00242   /// \arg AP - If given, an AsmPrinter to use for printing instructions.
00243   MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS,
00244                                 const TargetAsmInfo &TAI, AsmPrinter *AP = 0);
00245 
00246   // FIXME: These two may end up getting rolled into a single
00247   // createObjectStreamer interface, which implements the assembler backend, and
00248   // is parameterized on an output object file writer.
00249 
00250   /// createMachOStream - Create a machine code streamer which will generative
00251   /// Mach-O format object files.
00252   MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS);
00253 
00254   /// createELFStreamer - Create a machine code streamer which will generative
00255   /// ELF format object files.
00256   MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
00257 
00258 } // end namespace llvm
00259 
00260 #endif