MCStreamer.h revision 2303c9dd69476d045af7cee94e112dd77d22fd28
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/Support/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 TargetAsmBackend; 31 class TargetLoweringObjectFile; 32 class Twine; 33 class raw_ostream; 34 class formatted_raw_ostream; 35 36 /// MCStreamer - Streaming machine code generation interface. This interface 37 /// is intended to provide a programatic interface that is very similar to the 38 /// level that an assembler .s file provides. It has callbacks to emit bytes, 39 /// handle directives, etc. The implementation of this interface retains 40 /// state to know what the current section is etc. 41 /// 42 /// There are multiple implementations of this interface: one for writing out 43 /// a .s file, and implementations that write out .o files of various formats. 44 /// 45 class MCStreamer { 46 MCContext &Context; 47 48 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 49 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 50 51 protected: 52 MCStreamer(MCContext &Ctx); 53 54 /// CurSection - This is the current section code is being emitted to, it is 55 /// kept up to date by SwitchSection. 56 const MCSection *CurSection; 57 58 /// PrevSection - This is the previous section code is being emitted to, it 59 /// is kept up to date by SwitchSection. 60 const MCSection *PrevSection; 61 62 public: 63 virtual ~MCStreamer(); 64 65 MCContext &getContext() const { return Context; } 66 67 /// @name Assembly File Formatting. 68 /// @{ 69 70 /// isVerboseAsm - Return true if this streamer supports verbose assembly 71 /// and if it is enabled. 72 virtual bool isVerboseAsm() const { return false; } 73 74 /// hasRawTextSupport - Return true if this asm streamer supports emitting 75 /// unformatted text to the .s file with EmitRawText. 76 virtual bool hasRawTextSupport() const { return false; } 77 78 /// AddComment - Add a comment that can be emitted to the generated .s 79 /// file if applicable as a QoI issue to make the output of the compiler 80 /// more readable. This only affects the MCAsmStreamer, and only when 81 /// verbose assembly output is enabled. 82 /// 83 /// If the comment includes embedded \n's, they will each get the comment 84 /// prefix as appropriate. The added comment should not end with a \n. 85 virtual void AddComment(const Twine &T) {} 86 87 /// GetCommentOS - Return a raw_ostream that comments can be written to. 88 /// Unlike AddComment, you are required to terminate comments with \n if you 89 /// use this method. 90 virtual raw_ostream &GetCommentOS(); 91 92 /// AddBlankLine - Emit a blank line to a .s file to pretty it up. 93 virtual void AddBlankLine() {} 94 95 /// @} 96 97 /// @name Symbol & Section Management 98 /// @{ 99 100 /// getCurrentSection - Return the current section that the streamer is 101 /// emitting code to. 102 const MCSection *getCurrentSection() const { return CurSection; } 103 104 /// getPreviousSection - Return the previous section that the streamer is 105 /// emitting code to. 106 const MCSection *getPreviousSection() const { return PrevSection; } 107 108 /// SwitchSection - Set the current section where code is being emitted to 109 /// @p Section. This is required to update CurSection. 110 /// 111 /// This corresponds to assembler directives like .section, .text, etc. 112 virtual void SwitchSection(const MCSection *Section) = 0; 113 114 /// InitSections - Create the default sections and set the initial one. 115 virtual void InitSections() = 0; 116 117 /// EmitLabel - Emit a label for @p Symbol into the current section. 118 /// 119 /// This corresponds to an assembler statement such as: 120 /// foo: 121 /// 122 /// @param Symbol - The symbol to emit. A given symbol should only be 123 /// emitted as a label once, and symbols emitted as a label should never be 124 /// used in an assignment. 125 virtual void EmitLabel(MCSymbol *Symbol) = 0; 126 127 /// EmitAssemblerFlag - Note in the output the specified @p Flag 128 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0; 129 130 /// EmitThumbFunc - Note in the output that the specified @p Func is 131 /// a Thumb mode function (ARM target only). 132 virtual void EmitThumbFunc(MCSymbol *Func) = 0; 133 134 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol. 135 /// 136 /// This corresponds to an assembler statement such as: 137 /// symbol = value 138 /// 139 /// The assignment generates no code, but has the side effect of binding the 140 /// value in the current context. For the assembly streamer, this prints the 141 /// binding into the .s file. 142 /// 143 /// @param Symbol - The symbol being assigned to. 144 /// @param Value - The value for the symbol. 145 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0; 146 147 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol. 148 /// 149 /// This corresponds to an assembler statement such as: 150 /// .weakref alias, symbol 151 /// 152 /// @param Alias - The alias that is being created. 153 /// @param Symbol - The symbol being aliased. 154 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0; 155 156 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol. 157 virtual void EmitSymbolAttribute(MCSymbol *Symbol, 158 MCSymbolAttr Attribute) = 0; 159 160 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol. 161 /// 162 /// @param Symbol - The symbol to have its n_desc field set. 163 /// @param DescValue - The value to set into the n_desc field. 164 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0; 165 166 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition 167 /// 168 /// @param Symbol - The symbol to have its External & Type fields set. 169 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0; 170 171 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol. 172 /// 173 /// @param StorageClass - The storage class the symbol should have. 174 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0; 175 176 /// EmitCOFFSymbolType - Emit the type of the symbol. 177 /// 178 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h) 179 virtual void EmitCOFFSymbolType(int Type) = 0; 180 181 /// EndCOFFSymbolDef - Marks the end of the symbol definition. 182 virtual void EndCOFFSymbolDef() = 0; 183 184 /// EmitELFSize - Emit an ELF .size directive. 185 /// 186 /// This corresponds to an assembler statement such as: 187 /// .size symbol, expression 188 /// 189 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0; 190 191 /// EmitCommonSymbol - Emit a common symbol. 192 /// 193 /// @param Symbol - The common symbol to emit. 194 /// @param Size - The size of the common symbol. 195 /// @param ByteAlignment - The alignment of the symbol if 196 /// non-zero. This must be a power of 2. 197 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 198 unsigned ByteAlignment) = 0; 199 200 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol. 201 /// 202 /// @param Symbol - The common symbol to emit. 203 /// @param Size - The size of the common symbol. 204 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0; 205 206 /// EmitZerofill - Emit the zerofill section and an optional symbol. 207 /// 208 /// @param Section - The zerofill section to create and or to put the symbol 209 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 210 /// @param Size - The size of the zerofill symbol. 211 /// @param ByteAlignment - The alignment of the zerofill symbol if 212 /// non-zero. This must be a power of 2 on some targets. 213 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 214 unsigned Size = 0,unsigned ByteAlignment = 0) = 0; 215 216 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol. 217 /// 218 /// @param Section - The thread local common section. 219 /// @param Symbol - The thread local common symbol to emit. 220 /// @param Size - The size of the symbol. 221 /// @param ByteAlignment - The alignment of the thread local common symbol 222 /// if non-zero. This must be a power of 2 on some targets. 223 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, 224 uint64_t Size, unsigned ByteAlignment = 0) = 0; 225 /// @} 226 /// @name Generating Data 227 /// @{ 228 229 /// EmitBytes - Emit the bytes in \arg Data into the output. 230 /// 231 /// This is used to implement assembler directives such as .byte, .ascii, 232 /// etc. 233 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0; 234 235 /// EmitValue - Emit the expression @p Value into the output as a native 236 /// integer of the given @p Size bytes. 237 /// 238 /// This is used to implement assembler directives such as .word, .quad, 239 /// etc. 240 /// 241 /// @param Value - The value to emit. 242 /// @param Size - The size of the integer (in bytes) to emit. This must 243 /// match a native machine width. 244 virtual void EmitValue(const MCExpr *Value, unsigned Size, 245 unsigned AddrSpace = 0) = 0; 246 247 /// EmitIntValue - Special case of EmitValue that avoids the client having 248 /// to pass in a MCExpr for constant integers. 249 virtual void EmitIntValue(uint64_t Value, unsigned Size, 250 unsigned AddrSpace = 0); 251 252 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO 253 /// this is done by producing 254 /// foo = value 255 /// .long foo 256 void EmitAbsValue(const MCExpr *Value, unsigned Size); 257 258 virtual void EmitULEB128Value(const MCExpr *Value, 259 unsigned AddrSpace = 0) = 0; 260 261 virtual void EmitSLEB128Value(const MCExpr *Value, 262 unsigned AddrSpace = 0) = 0; 263 264 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the 265 /// client having to pass in a MCExpr for constant integers. 266 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0); 267 268 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the 269 /// client having to pass in a MCExpr for constant integers. 270 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0); 271 272 /// EmitSymbolValue - Special case of EmitValue that avoids the client 273 /// having to pass in a MCExpr for MCSymbols. 274 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, 275 unsigned AddrSpace = 0); 276 277 /// EmitGPRel32Value - Emit the expression @p Value into the output as a 278 /// gprel32 (32-bit GP relative) value. 279 /// 280 /// This is used to implement assembler directives such as .gprel32 on 281 /// targets that support them. 282 virtual void EmitGPRel32Value(const MCExpr *Value); 283 284 /// EmitFill - Emit NumBytes bytes worth of the value specified by 285 /// FillValue. This implements directives such as '.space'. 286 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 287 unsigned AddrSpace); 288 289 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience 290 /// function that just wraps EmitFill. 291 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) { 292 EmitFill(NumBytes, 0, AddrSpace); 293 } 294 295 296 /// EmitValueToAlignment - Emit some number of copies of @p Value until 297 /// the byte alignment @p ByteAlignment is reached. 298 /// 299 /// If the number of bytes need to emit for the alignment is not a multiple 300 /// of @p ValueSize, then the contents of the emitted fill bytes is 301 /// undefined. 302 /// 303 /// This used to implement the .align assembler directive. 304 /// 305 /// @param ByteAlignment - The alignment to reach. This must be a power of 306 /// two on some targets. 307 /// @param Value - The value to use when filling bytes. 308 /// @param ValueSize - The size of the integer (in bytes) to emit for 309 /// @p Value. This must match a native machine width. 310 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 311 /// the alignment cannot be reached in this many bytes, no bytes are 312 /// emitted. 313 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 314 unsigned ValueSize = 1, 315 unsigned MaxBytesToEmit = 0) = 0; 316 317 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment 318 /// is reached. 319 /// 320 /// This used to align code where the alignment bytes may be executed. This 321 /// can emit different bytes for different sizes to optimize execution. 322 /// 323 /// @param ByteAlignment - The alignment to reach. This must be a power of 324 /// two on some targets. 325 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 326 /// the alignment cannot be reached in this many bytes, no bytes are 327 /// emitted. 328 virtual void EmitCodeAlignment(unsigned ByteAlignment, 329 unsigned MaxBytesToEmit = 0) = 0; 330 331 /// EmitValueToOffset - Emit some number of copies of @p Value until the 332 /// byte offset @p Offset is reached. 333 /// 334 /// This is used to implement assembler directives such as .org. 335 /// 336 /// @param Offset - The offset to reach. This may be an expression, but the 337 /// expression must be associated with the current section. 338 /// @param Value - The value to use when filling bytes. 339 virtual void EmitValueToOffset(const MCExpr *Offset, 340 unsigned char Value = 0) = 0; 341 342 /// @} 343 344 /// EmitFileDirective - Switch to a new logical file. This is used to 345 /// implement the '.file "foo.c"' assembler directive. 346 virtual void EmitFileDirective(StringRef Filename) = 0; 347 348 /// EmitDwarfFileDirective - Associate a filename with a specified logical 349 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler 350 /// directive. 351 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename); 352 353 /// EmitDwarfLocDirective - This implements the DWARF2 354 // '.loc fileno lineno ...' assembler directive. 355 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, 356 unsigned Column, unsigned Flags, 357 unsigned Isa, 358 unsigned Discriminator); 359 360 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta, 361 const MCSymbol *LastLabel, 362 const MCSymbol *Label) = 0; 363 364 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label, 365 int PointerSize); 366 367 virtual bool EmitCFIStartProc(); 368 virtual bool EmitCFIEndProc(); 369 virtual bool EmitCFIDefCfaOffset(int64_t Offset); 370 virtual bool EmitCFIDefCfaRegister(int64_t Register); 371 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset); 372 virtual bool EmitCFIPersonality(const MCSymbol *Sym); 373 virtual bool EmitCFILsda(const MCSymbol *Sym); 374 375 /// EmitInstruction - Emit the given @p Instruction into the current 376 /// section. 377 virtual void EmitInstruction(const MCInst &Inst) = 0; 378 379 /// EmitRawText - If this file is backed by a assembly streamer, this dumps 380 /// the specified string in the output .s file. This capability is 381 /// indicated by the hasRawTextSupport() predicate. By default this aborts. 382 virtual void EmitRawText(StringRef String); 383 void EmitRawText(const Twine &String); 384 385 /// Finish - Finish emission of machine code. 386 virtual void Finish() = 0; 387 }; 388 389 /// createNullStreamer - Create a dummy machine code streamer, which does 390 /// nothing. This is useful for timing the assembler front end. 391 MCStreamer *createNullStreamer(MCContext &Ctx); 392 393 /// createAsmStreamer - Create a machine code streamer which will print out 394 /// assembly for the native target, suitable for compiling with a native 395 /// assembler. 396 /// 397 /// \param InstPrint - If given, the instruction printer to use. If not given 398 /// the MCInst representation will be printed. This method takes ownership of 399 /// InstPrint. 400 /// 401 /// \param CE - If given, a code emitter to use to show the instruction 402 /// encoding inline with the assembly. This method takes ownership of \arg CE. 403 /// 404 /// \param ShowInst - Whether to show the MCInst representation inline with 405 /// the assembly. 406 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, 407 bool isLittleEndian, bool isVerboseAsm, 408 MCInstPrinter *InstPrint = 0, 409 MCCodeEmitter *CE = 0, 410 bool ShowInst = false); 411 412 MCStreamer *createAsmStreamerNoLoc(MCContext &Ctx, formatted_raw_ostream &OS, 413 bool isLittleEndian, bool isVerboseAsm, 414 const TargetLoweringObjectFile *TLOF, 415 int PointerSize, 416 MCInstPrinter *InstPrint = 0, 417 MCCodeEmitter *CE = 0, 418 bool ShowInst = false); 419 420 /// createMachOStreamer - Create a machine code streamer which will generate 421 /// Mach-O format object files. 422 /// 423 /// Takes ownership of \arg TAB and \arg CE. 424 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB, 425 raw_ostream &OS, MCCodeEmitter *CE, 426 bool RelaxAll = false); 427 428 /// createWinCOFFStreamer - Create a machine code streamer which will 429 /// generate Microsoft COFF format object files. 430 /// 431 /// Takes ownership of \arg TAB and \arg CE. 432 MCStreamer *createWinCOFFStreamer(MCContext &Ctx, 433 TargetAsmBackend &TAB, 434 MCCodeEmitter &CE, raw_ostream &OS, 435 bool RelaxAll = false); 436 437 /// createELFStreamer - Create a machine code streamer which will generate 438 /// ELF format object files. 439 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB, 440 raw_ostream &OS, MCCodeEmitter *CE, 441 bool RelaxAll = false); 442 443 /// createLoggingStreamer - Create a machine code streamer which just logs the 444 /// API calls and then dispatches to another streamer. 445 /// 446 /// The new streamer takes ownership of the \arg Child. 447 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS); 448 449 /// createPureStreamer - Create a machine code streamer which will generate 450 /// "pure" MC object files, for use with MC-JIT and testing tools. 451 /// 452 /// Takes ownership of \arg TAB and \arg CE. 453 MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB, 454 raw_ostream &OS, MCCodeEmitter *CE); 455 456} // end namespace llvm 457 458#endif 459