MCStreamer.h revision 6e72048add2a6464e038121c6c275da37528aa0a
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#include "llvm/MC/MCDirectives.h" 19 20namespace llvm { 21 class MCAsmInfo; 22 class MCCodeEmitter; 23 class MCContext; 24 class MCExpr; 25 class MCInst; 26 class MCInstPrinter; 27 class MCSection; 28 class MCSymbol; 29 class StringRef; 30 class Twine; 31 class raw_ostream; 32 class formatted_raw_ostream; 33 34 /// MCStreamer - Streaming machine code generation interface. This interface 35 /// is intended to provide a programatic interface that is very similar to the 36 /// level that an assembler .s file provides. It has callbacks to emit bytes, 37 /// handle directives, etc. The implementation of this interface retains 38 /// state to know what the current section is etc. 39 /// 40 /// There are multiple implementations of this interface: one for writing out 41 /// a .s file, and implementations that write out .o files of various formats. 42 /// 43 class MCStreamer { 44 MCContext &Context; 45 46 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 47 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 48 49 protected: 50 MCStreamer(MCContext &Ctx); 51 52 /// CurSection - This is the current section code is being emitted to, it is 53 /// kept up to date by SwitchSection. 54 const MCSection *CurSection; 55 56 public: 57 virtual ~MCStreamer(); 58 59 MCContext &getContext() const { return Context; } 60 61 /// @name Assembly File Formatting. 62 /// @{ 63 64 /// isVerboseAsm - Return true if this streamer supports verbose assembly at 65 /// all. 66 virtual bool isVerboseAsm() const { return false; } 67 68 /// AddComment - Add a comment that can be emitted to the generated .s 69 /// file if applicable as a QoI issue to make the output of the compiler 70 /// more readable. This only affects the MCAsmStreamer, and only when 71 /// verbose assembly output is enabled. 72 /// 73 /// If the comment includes embedded \n's, they will each get the comment 74 /// prefix as appropriate. The added comment should not end with a \n. 75 virtual void AddComment(const Twine &T) {} 76 77 /// GetCommentOS - Return a raw_ostream that comments can be written to. 78 /// Unlike AddComment, you are required to terminate comments with \n if you 79 /// use this method. 80 virtual raw_ostream &GetCommentOS(); 81 82 /// AddBlankLine - Emit a blank line to a .s file to pretty it up. 83 virtual void AddBlankLine() {} 84 85 /// @} 86 87 /// @name Symbol & Section Management 88 /// @{ 89 90 /// getCurrentSection - Return the current seciton that the streamer is 91 /// emitting code to. 92 const MCSection *getCurrentSection() const { return CurSection; } 93 94 /// SwitchSection - Set the current section where code is being emitted to 95 /// @p Section. This is required to update CurSection. 96 /// 97 /// This corresponds to assembler directives like .section, .text, etc. 98 virtual void SwitchSection(const MCSection *Section) = 0; 99 100 /// EmitLabel - Emit a label for @p Symbol into the current section. 101 /// 102 /// This corresponds to an assembler statement such as: 103 /// foo: 104 /// 105 /// @param Symbol - The symbol to emit. A given symbol should only be 106 /// emitted as a label once, and symbols emitted as a label should never be 107 /// used in an assignment. 108 virtual void EmitLabel(MCSymbol *Symbol) = 0; 109 110 /// EmitAssemblerFlag - Note in the output the specified @p Flag 111 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0; 112 113 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol. 114 /// 115 /// This corresponds to an assembler statement such as: 116 /// symbol = value 117 /// 118 /// The assignment generates no code, but has the side effect of binding the 119 /// value in the current context. For the assembly streamer, this prints the 120 /// binding into the .s file. 121 /// 122 /// @param Symbol - The symbol being assigned to. 123 /// @param Value - The value for the symbol. 124 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0; 125 126 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol. 127 virtual void EmitSymbolAttribute(MCSymbol *Symbol, 128 MCSymbolAttr Attribute) = 0; 129 130 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol. 131 /// 132 /// @param Symbol - The symbol to have its n_desc field set. 133 /// @param DescValue - The value to set into the n_desc field. 134 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0; 135 136 137 /// EmitELFSize - Emit an ELF .size directive. 138 /// 139 /// This corresponds to an assembler statement such as: 140 /// .size symbol, expression 141 /// 142 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0; 143 144 /// EmitCommonSymbol - Emit a common symbol. 145 /// 146 /// @param Symbol - The common symbol to emit. 147 /// @param Size - The size of the common symbol. 148 /// @param ByteAlignment - The alignment of the symbol if 149 /// non-zero. This must be a power of 2. 150 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 151 unsigned ByteAlignment) = 0; 152 153 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol. 154 /// 155 /// @param Symbol - The common symbol to emit. 156 /// @param Size - The size of the common symbol. 157 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0; 158 159 /// EmitZerofill - Emit a the zerofill section and an option symbol. 160 /// 161 /// @param Section - The zerofill section to create and or to put the symbol 162 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 163 /// @param Size - The size of the zerofill symbol. 164 /// @param ByteAlignment - The alignment of the zerofill symbol if 165 /// non-zero. This must be a power of 2 on some targets. 166 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 167 unsigned Size = 0,unsigned ByteAlignment = 0) = 0; 168 169 /// @} 170 /// @name Generating Data 171 /// @{ 172 173 /// EmitBytes - Emit the bytes in \arg Data into the output. 174 /// 175 /// This is used to implement assembler directives such as .byte, .ascii, 176 /// etc. 177 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0; 178 179 /// EmitValue - Emit the expression @p Value into the output as a native 180 /// integer of the given @p Size bytes. 181 /// 182 /// This is used to implement assembler directives such as .word, .quad, 183 /// etc. 184 /// 185 /// @param Value - The value to emit. 186 /// @param Size - The size of the integer (in bytes) to emit. This must 187 /// match a native machine width. 188 virtual void EmitValue(const MCExpr *Value, unsigned Size, 189 unsigned AddrSpace) = 0; 190 191 /// EmitIntValue - Special case of EmitValue that avoids the client having 192 /// to pass in a MCExpr for constant integers. 193 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace); 194 195 /// EmitGPRel32Value - Emit the expression @p Value into the output as a 196 /// gprel32 (32-bit GP relative) value. 197 /// 198 /// This is used to implement assembler directives such as .gprel32 on 199 /// targets that support them. 200 virtual void EmitGPRel32Value(const MCExpr *Value) = 0; 201 202 /// EmitFill - Emit NumBytes bytes worth of the value specified by 203 /// FillValue. This implements directives such as '.space'. 204 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 205 unsigned AddrSpace); 206 207 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience 208 /// function that just wraps EmitFill. 209 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) { 210 EmitFill(NumBytes, 0, AddrSpace); 211 } 212 213 214 /// EmitValueToAlignment - Emit some number of copies of @p Value until 215 /// the byte alignment @p ByteAlignment is reached. 216 /// 217 /// If the number of bytes need to emit for the alignment is not a multiple 218 /// of @p ValueSize, then the contents of the emitted fill bytes is 219 /// undefined. 220 /// 221 /// This used to implement the .align assembler directive. 222 /// 223 /// @param ByteAlignment - The alignment to reach. This must be a power of 224 /// two on some targets. 225 /// @param Value - The value to use when filling bytes. 226 /// @param ValueSize - The size of the integer (in bytes) to emit for 227 /// @p Value. This must match a native machine width. 228 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 229 /// the alignment cannot be reached in this many bytes, no bytes are 230 /// emitted. 231 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 232 unsigned ValueSize = 1, 233 unsigned MaxBytesToEmit = 0) = 0; 234 235 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment 236 /// is reached. 237 /// 238 /// This used to align code where the alignment bytes may be executed. This 239 /// can emit different bytes for different sizes to optimize execution. 240 /// 241 /// @param ByteAlignment - The alignment to reach. This must be a power of 242 /// two on some targets. 243 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 244 /// the alignment cannot be reached in this many bytes, no bytes are 245 /// emitted. 246 virtual void EmitCodeAlignment(unsigned ByteAlignment, 247 unsigned MaxBytesToEmit = 0) = 0; 248 249 /// EmitValueToOffset - Emit some number of copies of @p Value until the 250 /// byte offset @p Offset is reached. 251 /// 252 /// This is used to implement assembler directives such as .org. 253 /// 254 /// @param Offset - The offset to reach. This may be an expression, but the 255 /// expression must be associated with the current section. 256 /// @param Value - The value to use when filling bytes. 257 virtual void EmitValueToOffset(const MCExpr *Offset, 258 unsigned char Value = 0) = 0; 259 260 /// @} 261 262 /// EmitFileDirective - Switch to a new logical file. This is used to 263 /// implement the '.file "foo.c"' assembler directive. 264 virtual void EmitFileDirective(StringRef Filename) = 0; 265 266 /// EmitDwarfFileDirective - Associate a filename with a specified logical 267 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler 268 /// directive. 269 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0; 270 271 /// EmitInstruction - Emit the given @p Instruction into the current 272 /// section. 273 virtual void EmitInstruction(const MCInst &Inst) = 0; 274 275 /// Finish - Finish emission of machine code and flush any output. 276 virtual void Finish() = 0; 277 }; 278 279 /// createNullStreamer - Create a dummy machine code streamer, which does 280 /// nothing. This is useful for timing the assembler front end. 281 MCStreamer *createNullStreamer(MCContext &Ctx); 282 283 /// createAsmStreamer - Create a machine code streamer which will print out 284 /// assembly for the native target, suitable for compiling with a native 285 /// assembler. 286 /// 287 /// \param InstPrint - If given, the instruction printer to use. If not given 288 /// the MCInst representation will be printed. 289 /// 290 /// \param CE - If given, a code emitter to use to show the instruction 291 /// encoding inline with the assembly. 292 /// 293 /// \param ShowInst - Whether to show the MCInst representation inline with 294 /// the assembly. 295 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, 296 const MCAsmInfo &MAI, bool isLittleEndian, 297 bool isVerboseAsm, 298 MCInstPrinter *InstPrint = 0, 299 MCCodeEmitter *CE = 0, 300 bool ShowInst = false); 301 302 // FIXME: These two may end up getting rolled into a single 303 // createObjectStreamer interface, which implements the assembler backend, and 304 // is parameterized on an output object file writer. 305 306 /// createMachOStream - Create a machine code streamer which will generative 307 /// Mach-O format object files. 308 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS, 309 MCCodeEmitter *CE); 310 311 /// createELFStreamer - Create a machine code streamer which will generative 312 /// ELF format object files. 313 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 314 315} // end namespace llvm 316 317#endif 318