MCStreamer.h revision 3924868a957d5a6d468b61741cbb7db77324d1f6
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 Local, /// .local (ELF) 51 NoDeadStrip, /// .no_dead_strip (Apple) 52 PrivateExtern, /// .private_extern (Apple) 53 Protected, /// .protected (ELF) 54 Reference, /// .reference (Apple) 55 Weak, /// .weak 56 WeakDefinition, /// .weak_definition (Apple) 57 WeakReference, /// .weak_reference (Apple) 58 59 SymbolAttrFirst = Global, 60 SymbolAttrLast = WeakReference 61 }; 62 63 enum AssemblerFlag { 64 SubsectionsViaSymbols /// .subsections_via_symbols (Apple) 65 }; 66 67 private: 68 MCContext &Context; 69 70 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 71 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 72 73 protected: 74 MCStreamer(MCContext &Ctx); 75 76 /// CurSection - This is the current section code is being emitted to, it is 77 /// kept up to date by SwitchSection. 78 const MCSection *CurSection; 79 80 public: 81 virtual ~MCStreamer(); 82 83 MCContext &getContext() const { return Context; } 84 85 /// @name Assembly File Formatting. 86 /// @{ 87 88 /// AddComment - Add a comment that can be emitted to the generated .s 89 /// file if applicable as a QoI issue to make the output of the compiler 90 /// more readable. This only affects the MCAsmStreamer, and only when 91 /// verbose assembly output is enabled. 92 /// 93 /// If the comment includes embedded \n's, they will each get the comment 94 /// prefix as appropriate. The added comment should not end with a \n. 95 virtual void AddComment(const Twine &T) {} 96 97 /// GetCommentOS - Return a raw_ostream that comments can be written to. 98 /// Unlike AddComment, you are required to terminate comments with \n if you 99 /// use this method. 100 virtual raw_ostream &GetCommentOS(); 101 102 /// AddBlankLine - Emit a blank line to a .s file to pretty it up. 103 virtual void AddBlankLine() {} 104 105 /// @} 106 107 /// @name Symbol & Section Management 108 /// @{ 109 110 /// getCurrentSection - Return the current seciton that the streamer is 111 /// emitting code to. 112 const MCSection *getCurrentSection() const { return CurSection; } 113 114 /// SwitchSection - Set the current section where code is being emitted to 115 /// @param Section. This is required to update CurSection. 116 /// 117 /// This corresponds to assembler directives like .section, .text, etc. 118 virtual void SwitchSection(const MCSection *Section) = 0; 119 120 /// EmitLabel - Emit a label for @param Symbol into the current section. 121 /// 122 /// This corresponds to an assembler statement such as: 123 /// foo: 124 /// 125 /// @param Symbol - The symbol to emit. A given symbol should only be 126 /// emitted as a label once, and symbols emitted as a label should never be 127 /// used in an assignment. 128 virtual void EmitLabel(MCSymbol *Symbol) = 0; 129 130 /// EmitAssemblerFlag - Note in the output the specified @param Flag 131 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0; 132 133 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol. 134 /// 135 /// This corresponds to an assembler statement such as: 136 /// symbol = value 137 /// 138 /// The assignment generates no code, but has the side effect of binding the 139 /// value in the current context. For the assembly streamer, this prints the 140 /// binding into the .s file. 141 /// 142 /// @param Symbol - The symbol being assigned to. 143 /// @param Value - The value for the symbol. 144 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0; 145 146 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol. 147 virtual void EmitSymbolAttribute(MCSymbol *Symbol, 148 SymbolAttr Attribute) = 0; 149 150 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol. 151 /// 152 /// @param Symbol - The symbol to have its n_desc field set. 153 /// @param DescValue - The value to set into the n_desc field. 154 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0; 155 156 /// EmitCommonSymbol - Emit a common or local common symbol. 157 /// 158 /// @param Symbol - The common symbol to emit. 159 /// @param Size - The size of the common symbol. 160 /// @param ByteAlignment - The alignment of the symbol if 161 /// non-zero. This must be a power of 2 on some targets. 162 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 163 unsigned ByteAlignment) = 0; 164 165 /// EmitZerofill - Emit a the zerofill section and an option symbol. 166 /// 167 /// @param Section - The zerofill section to create and or to put the symbol 168 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 169 /// @param Size - The size of the zerofill symbol. 170 /// @param ByteAlignment - The alignment of the zerofill symbol if 171 /// non-zero. This must be a power of 2 on some targets. 172 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 173 unsigned Size = 0,unsigned ByteAlignment = 0) = 0; 174 175 /// @} 176 /// @name Generating Data 177 /// @{ 178 179 /// EmitBytes - Emit the bytes in \arg Data into the output. 180 /// 181 /// This is used to implement assembler directives such as .byte, .ascii, 182 /// etc. 183 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0; 184 185 /// EmitValue - Emit the expression @param Value into the output as a native 186 /// integer of the given @param Size bytes. 187 /// 188 /// This is used to implement assembler directives such as .word, .quad, 189 /// etc. 190 /// 191 /// @param Value - The value to emit. 192 /// @param Size - The size of the integer (in bytes) to emit. This must 193 /// match a native machine width. 194 virtual void EmitValue(const MCExpr *Value, unsigned Size, 195 unsigned AddrSpace) = 0; 196 197 /// EmitIntValue - Special case of EmitValue that avoids the client having 198 /// to pass in a MCExpr for constant integers. 199 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace); 200 201 /// EmitFill - Emit NumBytes bytes worth of the value specified by 202 /// FillValue. This implements directives such as '.space'. 203 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 204 unsigned AddrSpace); 205 206 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience 207 /// function that just wraps EmitFill. 208 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) { 209 EmitFill(NumBytes, 0, AddrSpace); 210 } 211 212 213 /// EmitValueToAlignment - Emit some number of copies of @param Value until 214 /// the byte alignment @param ByteAlignment is reached. 215 /// 216 /// If the number of bytes need to emit for the alignment is not a multiple 217 /// of @param ValueSize, then the contents of the emitted fill bytes is 218 /// undefined. 219 /// 220 /// This used to implement the .align assembler directive. 221 /// 222 /// @param ByteAlignment - The alignment to reach. This must be a power of 223 /// two on some targets. 224 /// @param Value - The value to use when filling bytes. 225 /// @param Size - The size of the integer (in bytes) to emit for @param 226 /// Value. This must match a native machine width. 227 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 228 /// the alignment cannot be reached in this many bytes, no bytes are 229 /// emitted. 230 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 231 unsigned ValueSize = 1, 232 unsigned MaxBytesToEmit = 0) = 0; 233 234 /// EmitValueToOffset - Emit some number of copies of @param Value until the 235 /// byte offset @param Offset is reached. 236 /// 237 /// This is used to implement assembler directives such as .org. 238 /// 239 /// @param Offset - The offset to reach. This may be an expression, but the 240 /// expression must be associated with the current section. 241 /// @param Value - The value to use when filling bytes. 242 virtual void EmitValueToOffset(const MCExpr *Offset, 243 unsigned char Value = 0) = 0; 244 245 /// @} 246 247 /// EmitInstruction - Emit the given @param Instruction into the current 248 /// section. 249 virtual void EmitInstruction(const MCInst &Inst) = 0; 250 251 /// Finish - Finish emission of machine code and flush any output. 252 virtual void Finish() = 0; 253 }; 254 255 /// createNullStreamer - Create a dummy machine code streamer, which does 256 /// nothing. This is useful for timing the assembler front end. 257 MCStreamer *createNullStreamer(MCContext &Ctx); 258 259 /// createAsmStreamer - Create a machine code streamer which will print out 260 /// assembly for the native target, suitable for compiling with a native 261 /// assembler. 262 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, 263 const MCAsmInfo &MAI, bool isLittleEndian, 264 bool isVerboseAsm, 265 MCInstPrinter *InstPrint = 0, 266 MCCodeEmitter *CE = 0); 267 268 // FIXME: These two may end up getting rolled into a single 269 // createObjectStreamer interface, which implements the assembler backend, and 270 // is parameterized on an output object file writer. 271 272 /// createMachOStream - Create a machine code streamer which will generative 273 /// Mach-O format object files. 274 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS, 275 MCCodeEmitter *CE = 0); 276 277 /// createELFStreamer - Create a machine code streamer which will generative 278 /// ELF format object files. 279 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 280 281} // end namespace llvm 282 283#endif 284