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