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