MCStreamer.h revision 6cde3e6e993126df756e3be5b9ef43540b904644
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file declares the MCStreamer class. 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_MC_MCSTREAMER_H 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_MC_MCSTREAMER_H 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/System/DataTypes.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/MC/MCDirectives.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCAsmInfo; 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCCodeEmitter; 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCContext; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCExpr; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCInst; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCInstPrinter; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCSection; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCSymbol; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class StringRef; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class Twine; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class raw_ostream; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class formatted_raw_ostream; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// MCStreamer - Streaming machine code generation interface. This interface 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// is intended to provide a programatic interface that is very similar to the 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// level that an assembler .s file provides. It has callbacks to emit bytes, 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// handle directives, etc. The implementation of this interface retains 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// state to know what the current section is etc. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// There are multiple implementations of this interface: one for writing out 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// a .s file, and implementations that write out .o files of various formats. 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class MCStreamer { 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCContext &Context; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCStreamer(MCContext &Ctx); 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// CurSection - This is the current section code is being emitted to, it is 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// kept up to date by SwitchSection. 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const MCSection *CurSection; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~MCStreamer(); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MCContext &getContext() const { return Context; } 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @name Assembly File Formatting. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @{ 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// isVerboseAsm - Return true if this streamer supports verbose assembly at 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// all. 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool isVerboseAsm() const { return false; } 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// AddComment - Add a comment that can be emitted to the generated .s 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// file if applicable as a QoI issue to make the output of the compiler 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// more readable. This only affects the MCAsmStreamer, and only when 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// verbose assembly output is enabled. 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// If the comment includes embedded \n's, they will each get the comment 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// prefix as appropriate. The added comment should not end with a \n. 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AddComment(const Twine &T) {} 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// GetCommentOS - Return a raw_ostream that comments can be written to. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Unlike AddComment, you are required to terminate comments with \n if you 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// use this method. 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual raw_ostream &GetCommentOS(); 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// AddBlankLine - Emit a blank line to a .s file to pretty it up. 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void AddBlankLine() {} 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// @} 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @name Symbol & Section Management 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @{ 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// getCurrentSection - Return the current seciton that the streamer is 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// emitting code to. 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 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 /// EmitSymbolValue - Special case of EmitValue that avoids the client 196 /// having to pass in a MCExpr for MCSymbols. 197 virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, 198 unsigned AddrSpace); 199 200 /// EmitGPRel32Value - Emit the expression @p Value into the output as a 201 /// gprel32 (32-bit GP relative) value. 202 /// 203 /// This is used to implement assembler directives such as .gprel32 on 204 /// targets that support them. 205 virtual void EmitGPRel32Value(const MCExpr *Value) = 0; 206 207 /// EmitFill - Emit NumBytes bytes worth of the value specified by 208 /// FillValue. This implements directives such as '.space'. 209 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 210 unsigned AddrSpace); 211 212 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience 213 /// function that just wraps EmitFill. 214 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) { 215 EmitFill(NumBytes, 0, AddrSpace); 216 } 217 218 219 /// EmitValueToAlignment - Emit some number of copies of @p Value until 220 /// the byte alignment @p ByteAlignment is reached. 221 /// 222 /// If the number of bytes need to emit for the alignment is not a multiple 223 /// of @p ValueSize, then the contents of the emitted fill bytes is 224 /// undefined. 225 /// 226 /// This used to implement the .align assembler directive. 227 /// 228 /// @param ByteAlignment - The alignment to reach. This must be a power of 229 /// two on some targets. 230 /// @param Value - The value to use when filling bytes. 231 /// @param ValueSize - The size of the integer (in bytes) to emit for 232 /// @p Value. This must match a native machine width. 233 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 234 /// the alignment cannot be reached in this many bytes, no bytes are 235 /// emitted. 236 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 237 unsigned ValueSize = 1, 238 unsigned MaxBytesToEmit = 0) = 0; 239 240 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment 241 /// is reached. 242 /// 243 /// This used to align code where the alignment bytes may be executed. This 244 /// can emit different bytes for different sizes to optimize execution. 245 /// 246 /// @param ByteAlignment - The alignment to reach. This must be a power of 247 /// two on some targets. 248 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 249 /// the alignment cannot be reached in this many bytes, no bytes are 250 /// emitted. 251 virtual void EmitCodeAlignment(unsigned ByteAlignment, 252 unsigned MaxBytesToEmit = 0) = 0; 253 254 /// EmitValueToOffset - Emit some number of copies of @p Value until the 255 /// byte offset @p Offset is reached. 256 /// 257 /// This is used to implement assembler directives such as .org. 258 /// 259 /// @param Offset - The offset to reach. This may be an expression, but the 260 /// expression must be associated with the current section. 261 /// @param Value - The value to use when filling bytes. 262 virtual void EmitValueToOffset(const MCExpr *Offset, 263 unsigned char Value = 0) = 0; 264 265 /// @} 266 267 /// EmitFileDirective - Switch to a new logical file. This is used to 268 /// implement the '.file "foo.c"' assembler directive. 269 virtual void EmitFileDirective(StringRef Filename) = 0; 270 271 /// EmitDwarfFileDirective - Associate a filename with a specified logical 272 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler 273 /// directive. 274 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0; 275 276 /// EmitInstruction - Emit the given @p Instruction into the current 277 /// section. 278 virtual void EmitInstruction(const MCInst &Inst) = 0; 279 280 /// Finish - Finish emission of machine code and flush any output. 281 virtual void Finish() = 0; 282 }; 283 284 /// createNullStreamer - Create a dummy machine code streamer, which does 285 /// nothing. This is useful for timing the assembler front end. 286 MCStreamer *createNullStreamer(MCContext &Ctx); 287 288 /// createAsmStreamer - Create a machine code streamer which will print out 289 /// assembly for the native target, suitable for compiling with a native 290 /// assembler. 291 /// 292 /// \param InstPrint - If given, the instruction printer to use. If not given 293 /// the MCInst representation will be printed. 294 /// 295 /// \param CE - If given, a code emitter to use to show the instruction 296 /// encoding inline with the assembly. 297 /// 298 /// \param ShowInst - Whether to show the MCInst representation inline with 299 /// the assembly. 300 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, 301 const MCAsmInfo &MAI, bool isLittleEndian, 302 bool isVerboseAsm, 303 MCInstPrinter *InstPrint = 0, 304 MCCodeEmitter *CE = 0, 305 bool ShowInst = false); 306 307 // FIXME: These two may end up getting rolled into a single 308 // createObjectStreamer interface, which implements the assembler backend, and 309 // is parameterized on an output object file writer. 310 311 /// createMachOStream - Create a machine code streamer which will generative 312 /// Mach-O format object files. 313 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS, 314 MCCodeEmitter *CE); 315 316 /// createELFStreamer - Create a machine code streamer which will generative 317 /// ELF format object files. 318 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 319 320} // end namespace llvm 321 322#endif 323