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