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