MCStreamer.h revision 86e2211d0a496f470ea1d320161c8dc43593c5c6
1//===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the MCStreamer class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_MC_MCSTREAMER_H 15#define LLVM_MC_MCSTREAMER_H 16 17#include "llvm/System/DataTypes.h" 18 19namespace llvm { 20 class MCAsmInfo; 21 class MCCodeEmitter; 22 class MCContext; 23 class MCExpr; 24 class MCInst; 25 class MCInstPrinter; 26 class MCSection; 27 class MCSymbol; 28 class StringRef; 29 class Twine; 30 class raw_ostream; 31 class formatted_raw_ostream; 32 33 /// MCStreamer - Streaming machine code generation interface. This interface 34 /// is intended to provide a programatic interface that is very similar to the 35 /// level that an assembler .s file provides. It has callbacks to emit bytes, 36 /// handle directives, etc. The implementation of this interface retains 37 /// state to know what the current section is etc. 38 /// 39 /// There are multiple implementations of this interface: one for writing out 40 /// a .s file, and implementations that write out .o files of various formats. 41 /// 42 class MCStreamer { 43 public: 44 enum SymbolAttr { 45 Global, /// .globl 46 Hidden, /// .hidden (ELF) 47 IndirectSymbol, /// .indirect_symbol (Apple) 48 Internal, /// .internal (ELF) 49 LazyReference, /// .lazy_reference (Apple) 50 NoDeadStrip, /// .no_dead_strip (Apple) 51 PrivateExtern, /// .private_extern (Apple) 52 Protected, /// .protected (ELF) 53 Reference, /// .reference (Apple) 54 Weak, /// .weak 55 WeakDefinition, /// .weak_definition (Apple) 56 WeakReference, /// .weak_reference (Apple) 57 58 SymbolAttrFirst = Global, 59 SymbolAttrLast = WeakReference 60 }; 61 62 enum AssemblerFlag { 63 SubsectionsViaSymbols /// .subsections_via_symbols (Apple) 64 }; 65 66 private: 67 MCContext &Context; 68 69 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 70 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 71 72 protected: 73 MCStreamer(MCContext &Ctx); 74 75 /// CurSection - This is the current section code is being emitted to, it is 76 /// kept up to date by SwitchSection. 77 const MCSection *CurSection; 78 79 public: 80 virtual ~MCStreamer(); 81 82 MCContext &getContext() const { return Context; } 83 84 /// addComment - Add a comment that can be emitted to the generated .s 85 /// file if applicable as a QoI issue to make the output of the compiler 86 /// more readable. This only affects the MCAsmStreamer, and only when 87 /// verbose assembly output is enabled. 88 /// 89 /// If the comment includes embedded \n's, they will each get the comment 90 /// prefix as appropriate. The added comment should not end with a \n. 91 virtual void addComment(const Twine &T) {} 92 93 /// @name Symbol & Section Management 94 /// @{ 95 96 /// getCurrentSection - Return the current seciton that the streamer is 97 /// emitting code to. 98 const MCSection *getCurrentSection() const { return CurSection; } 99 100 /// SwitchSection - Set the current section where code is being emitted to 101 /// @param Section. This is required to update CurSection. 102 /// 103 /// This corresponds to assembler directives like .section, .text, etc. 104 virtual void SwitchSection(const MCSection *Section) = 0; 105 106 /// EmitLabel - Emit a label for @param Symbol into the current section. 107 /// 108 /// This corresponds to an assembler statement such as: 109 /// foo: 110 /// 111 /// @param Symbol - The symbol to emit. A given symbol should only be 112 /// emitted as a label once, and symbols emitted as a label should never be 113 /// used in an assignment. 114 virtual void EmitLabel(MCSymbol *Symbol) = 0; 115 116 /// EmitAssemblerFlag - Note in the output the specified @param Flag 117 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0; 118 119 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol. 120 /// 121 /// This corresponds to an assembler statement such as: 122 /// symbol = value 123 /// 124 /// The assignment generates no code, but has the side effect of binding the 125 /// value in the current context. For the assembly streamer, this prints the 126 /// binding into the .s file. 127 /// 128 /// @param Symbol - The symbol being assigned to. 129 /// @param Value - The value for the symbol. 130 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0; 131 132 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol. 133 virtual void EmitSymbolAttribute(MCSymbol *Symbol, 134 SymbolAttr Attribute) = 0; 135 136 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol. 137 /// 138 /// @param Symbol - The symbol to have its n_desc field set. 139 /// @param DescValue - The value to set into the n_desc field. 140 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0; 141 142 /// EmitCommonSymbol - Emit a common or local common symbol. 143 /// 144 /// @param Symbol - The common symbol to emit. 145 /// @param Size - The size of the common symbol. 146 /// @param ByteAlignment - The alignment of the symbol if 147 /// non-zero. This must be a power of 2 on some targets. 148 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 149 unsigned ByteAlignment) = 0; 150 151 /// EmitZerofill - Emit a the zerofill section and an option symbol. 152 /// 153 /// @param Section - The zerofill section to create and or to put the symbol 154 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 155 /// @param Size - The size of the zerofill symbol. 156 /// @param ByteAlignment - The alignment of the zerofill symbol if 157 /// non-zero. This must be a power of 2 on some targets. 158 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 159 unsigned Size = 0,unsigned ByteAlignment = 0) = 0; 160 161 /// @} 162 /// @name Generating Data 163 /// @{ 164 165 /// EmitBytes - Emit the bytes in \arg Data into the output. 166 /// 167 /// This is used to implement assembler directives such as .byte, .ascii, 168 /// etc. 169 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0; 170 171 /// EmitValue - Emit the expression @param Value into the output as a native 172 /// integer of the given @param Size bytes. 173 /// 174 /// This is used to implement assembler directives such as .word, .quad, 175 /// etc. 176 /// 177 /// @param Value - The value to emit. 178 /// @param Size - The size of the integer (in bytes) to emit. This must 179 /// match a native machine width. 180 virtual void EmitValue(const MCExpr *Value, unsigned Size, 181 unsigned AddrSpace) = 0; 182 183 /// EmitIntValue - Special case of EmitValue that avoids the client having 184 /// to pass in a MCExpr for constant integers. 185 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace); 186 187 /// EmitFill - Emit NumBytes bytes worth of the value specified by 188 /// FillValue. This implements directives such as '.space'. 189 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 190 unsigned AddrSpace); 191 192 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience 193 /// function that just wraps EmitFill. 194 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) { 195 EmitFill(NumBytes, 0, AddrSpace); 196 } 197 198 199 /// EmitValueToAlignment - Emit some number of copies of @param Value until 200 /// the byte alignment @param ByteAlignment is reached. 201 /// 202 /// If the number of bytes need to emit for the alignment is not a multiple 203 /// of @param ValueSize, then the contents of the emitted fill bytes is 204 /// undefined. 205 /// 206 /// This used to implement the .align assembler directive. 207 /// 208 /// @param ByteAlignment - The alignment to reach. This must be a power of 209 /// two on some targets. 210 /// @param Value - The value to use when filling bytes. 211 /// @param Size - The size of the integer (in bytes) to emit for @param 212 /// Value. This must match a native machine width. 213 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 214 /// the alignment cannot be reached in this many bytes, no bytes are 215 /// emitted. 216 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 217 unsigned ValueSize = 1, 218 unsigned MaxBytesToEmit = 0) = 0; 219 220 /// EmitValueToOffset - Emit some number of copies of @param Value until the 221 /// byte offset @param Offset is reached. 222 /// 223 /// This is used to implement assembler directives such as .org. 224 /// 225 /// @param Offset - The offset to reach. This may be an expression, but the 226 /// expression must be associated with the current section. 227 /// @param Value - The value to use when filling bytes. 228 virtual void EmitValueToOffset(const MCExpr *Offset, 229 unsigned char Value = 0) = 0; 230 231 /// @} 232 233 /// EmitInstruction - Emit the given @param Instruction into the current 234 /// section. 235 virtual void EmitInstruction(const MCInst &Inst) = 0; 236 237 /// Finish - Finish emission of machine code and flush any output. 238 virtual void Finish() = 0; 239 }; 240 241 /// createNullStreamer - Create a dummy machine code streamer, which does 242 /// nothing. This is useful for timing the assembler front end. 243 MCStreamer *createNullStreamer(MCContext &Ctx); 244 245 /// createAsmStreamer - Create a machine code streamer which will print out 246 /// assembly for the native target, suitable for compiling with a native 247 /// assembler. 248 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, 249 const MCAsmInfo &MAI, bool isLittleEndian, 250 bool isVerboseAsm, 251 MCInstPrinter *InstPrint = 0, 252 MCCodeEmitter *CE = 0); 253 254 // FIXME: These two may end up getting rolled into a single 255 // createObjectStreamer interface, which implements the assembler backend, and 256 // is parameterized on an output object file writer. 257 258 /// createMachOStream - Create a machine code streamer which will generative 259 /// Mach-O format object files. 260 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS, 261 MCCodeEmitter *CE = 0); 262 263 /// createELFStreamer - Create a machine code streamer which will generative 264 /// ELF format object files. 265 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 266 267} // end namespace llvm 268 269#endif 270