MCAsmStreamer.cpp revision 212904
1//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===// 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#include "llvm/MC/MCStreamer.h" 11#include "llvm/MC/MCAsmInfo.h" 12#include "llvm/MC/MCCodeEmitter.h" 13#include "llvm/MC/MCContext.h" 14#include "llvm/MC/MCExpr.h" 15#include "llvm/MC/MCInst.h" 16#include "llvm/MC/MCInstPrinter.h" 17#include "llvm/MC/MCSectionMachO.h" 18#include "llvm/MC/MCSymbol.h" 19#include "llvm/ADT/OwningPtr.h" 20#include "llvm/ADT/SmallString.h" 21#include "llvm/ADT/Twine.h" 22#include "llvm/Support/ErrorHandling.h" 23#include "llvm/Support/MathExtras.h" 24#include "llvm/Support/Format.h" 25#include "llvm/Support/FormattedStream.h" 26using namespace llvm; 27 28namespace { 29 30class MCAsmStreamer : public MCStreamer { 31 formatted_raw_ostream &OS; 32 const MCAsmInfo &MAI; 33 OwningPtr<MCInstPrinter> InstPrinter; 34 OwningPtr<MCCodeEmitter> Emitter; 35 36 SmallString<128> CommentToEmit; 37 raw_svector_ostream CommentStream; 38 39 unsigned IsLittleEndian : 1; 40 unsigned IsVerboseAsm : 1; 41 unsigned ShowInst : 1; 42 43public: 44 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os, 45 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer, 46 MCCodeEmitter *emitter, bool showInst) 47 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()), 48 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit), 49 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm), 50 ShowInst(showInst) { 51 if (InstPrinter && IsVerboseAsm) 52 InstPrinter->setCommentStream(CommentStream); 53 } 54 ~MCAsmStreamer() {} 55 56 bool isLittleEndian() const { return IsLittleEndian; } 57 58 inline void EmitEOL() { 59 // If we don't have any comments, just emit a \n. 60 if (!IsVerboseAsm) { 61 OS << '\n'; 62 return; 63 } 64 EmitCommentsAndEOL(); 65 } 66 void EmitCommentsAndEOL(); 67 68 /// isVerboseAsm - Return true if this streamer supports verbose assembly at 69 /// all. 70 virtual bool isVerboseAsm() const { return IsVerboseAsm; } 71 72 /// hasRawTextSupport - We support EmitRawText. 73 virtual bool hasRawTextSupport() const { return true; } 74 75 /// AddComment - Add a comment that can be emitted to the generated .s 76 /// file if applicable as a QoI issue to make the output of the compiler 77 /// more readable. This only affects the MCAsmStreamer, and only when 78 /// verbose assembly output is enabled. 79 virtual void AddComment(const Twine &T); 80 81 /// AddEncodingComment - Add a comment showing the encoding of an instruction. 82 virtual void AddEncodingComment(const MCInst &Inst); 83 84 /// GetCommentOS - Return a raw_ostream that comments can be written to. 85 /// Unlike AddComment, you are required to terminate comments with \n if you 86 /// use this method. 87 virtual raw_ostream &GetCommentOS() { 88 if (!IsVerboseAsm) 89 return nulls(); // Discard comments unless in verbose asm mode. 90 return CommentStream; 91 } 92 93 /// AddBlankLine - Emit a blank line to a .s file to pretty it up. 94 virtual void AddBlankLine() { 95 EmitEOL(); 96 } 97 98 /// @name MCStreamer Interface 99 /// @{ 100 101 virtual void SwitchSection(const MCSection *Section); 102 103 virtual void EmitLabel(MCSymbol *Symbol); 104 105 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); 106 107 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); 108 109 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); 110 111 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 112 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol); 113 virtual void EmitCOFFSymbolStorageClass(int StorageClass); 114 virtual void EmitCOFFSymbolType(int Type); 115 virtual void EndCOFFSymbolDef(); 116 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value); 117 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 118 unsigned ByteAlignment); 119 120 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol. 121 /// 122 /// @param Symbol - The common symbol to emit. 123 /// @param Size - The size of the common symbol. 124 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size); 125 126 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 127 unsigned Size = 0, unsigned ByteAlignment = 0); 128 129 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol, 130 uint64_t Size, unsigned ByteAlignment = 0); 131 132 virtual void EmitBytes(StringRef Data, unsigned AddrSpace); 133 134 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); 135 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace); 136 virtual void EmitGPRel32Value(const MCExpr *Value); 137 138 139 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 140 unsigned AddrSpace); 141 142 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 143 unsigned ValueSize = 1, 144 unsigned MaxBytesToEmit = 0); 145 146 virtual void EmitCodeAlignment(unsigned ByteAlignment, 147 unsigned MaxBytesToEmit = 0); 148 149 virtual void EmitValueToOffset(const MCExpr *Offset, 150 unsigned char Value = 0); 151 152 virtual void EmitFileDirective(StringRef Filename); 153 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename); 154 155 virtual void EmitInstruction(const MCInst &Inst); 156 157 /// EmitRawText - If this file is backed by a assembly streamer, this dumps 158 /// the specified string in the output .s file. This capability is 159 /// indicated by the hasRawTextSupport() predicate. 160 virtual void EmitRawText(StringRef String); 161 162 virtual void Finish(); 163 164 /// @} 165}; 166 167} // end anonymous namespace. 168 169/// AddComment - Add a comment that can be emitted to the generated .s 170/// file if applicable as a QoI issue to make the output of the compiler 171/// more readable. This only affects the MCAsmStreamer, and only when 172/// verbose assembly output is enabled. 173void MCAsmStreamer::AddComment(const Twine &T) { 174 if (!IsVerboseAsm) return; 175 176 // Make sure that CommentStream is flushed. 177 CommentStream.flush(); 178 179 T.toVector(CommentToEmit); 180 // Each comment goes on its own line. 181 CommentToEmit.push_back('\n'); 182 183 // Tell the comment stream that the vector changed underneath it. 184 CommentStream.resync(); 185} 186 187void MCAsmStreamer::EmitCommentsAndEOL() { 188 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) { 189 OS << '\n'; 190 return; 191 } 192 193 CommentStream.flush(); 194 StringRef Comments = CommentToEmit.str(); 195 196 assert(Comments.back() == '\n' && 197 "Comment array not newline terminated"); 198 do { 199 // Emit a line of comments. 200 OS.PadToColumn(MAI.getCommentColumn()); 201 size_t Position = Comments.find('\n'); 202 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n'; 203 204 Comments = Comments.substr(Position+1); 205 } while (!Comments.empty()); 206 207 CommentToEmit.clear(); 208 // Tell the comment stream that the vector changed underneath it. 209 CommentStream.resync(); 210} 211 212static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { 213 assert(Bytes && "Invalid size!"); 214 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); 215} 216 217void MCAsmStreamer::SwitchSection(const MCSection *Section) { 218 assert(Section && "Cannot switch to a null section!"); 219 if (Section != CurSection) { 220 PrevSection = CurSection; 221 CurSection = Section; 222 Section->PrintSwitchToSection(MAI, OS); 223 } 224} 225 226void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { 227 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 228 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 229 assert(CurSection && "Cannot emit before setting section!"); 230 231 OS << *Symbol << ":"; 232 EmitEOL(); 233 Symbol->setSection(*CurSection); 234} 235 236void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { 237 switch (Flag) { 238 default: assert(0 && "Invalid flag!"); 239 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break; 240 } 241 EmitEOL(); 242} 243 244void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 245 OS << *Symbol << " = " << *Value; 246 EmitEOL(); 247 248 // FIXME: Lift context changes into super class. 249 Symbol->setVariableValue(Value); 250} 251 252void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 253 MCSymbolAttr Attribute) { 254 switch (Attribute) { 255 case MCSA_Invalid: assert(0 && "Invalid symbol attribute"); 256 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function 257 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC 258 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object 259 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object 260 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common 261 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype 262 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported"); 263 OS << "\t.type\t" << *Symbol << ',' 264 << ((MAI.getCommentString()[0] != '@') ? '@' : '%'); 265 switch (Attribute) { 266 default: assert(0 && "Unknown ELF .type"); 267 case MCSA_ELF_TypeFunction: OS << "function"; break; 268 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break; 269 case MCSA_ELF_TypeObject: OS << "object"; break; 270 case MCSA_ELF_TypeTLS: OS << "tls_object"; break; 271 case MCSA_ELF_TypeCommon: OS << "common"; break; 272 case MCSA_ELF_TypeNoType: OS << "no_type"; break; 273 } 274 EmitEOL(); 275 return; 276 case MCSA_Global: // .globl/.global 277 OS << MAI.getGlobalDirective(); 278 break; 279 case MCSA_Hidden: OS << "\t.hidden\t"; break; 280 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break; 281 case MCSA_Internal: OS << "\t.internal\t"; break; 282 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break; 283 case MCSA_Local: OS << "\t.local\t"; break; 284 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break; 285 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break; 286 case MCSA_Protected: OS << "\t.protected\t"; break; 287 case MCSA_Reference: OS << "\t.reference\t"; break; 288 case MCSA_Weak: OS << "\t.weak\t"; break; 289 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break; 290 // .weak_reference 291 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break; 292 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break; 293 } 294 295 OS << *Symbol; 296 EmitEOL(); 297} 298 299void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 300 OS << ".desc" << ' ' << *Symbol << ',' << DescValue; 301 EmitEOL(); 302} 303 304void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { 305 OS << "\t.def\t " << *Symbol << ';'; 306 EmitEOL(); 307} 308 309void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) { 310 OS << "\t.scl\t" << StorageClass << ';'; 311 EmitEOL(); 312} 313 314void MCAsmStreamer::EmitCOFFSymbolType (int Type) { 315 OS << "\t.type\t" << Type << ';'; 316 EmitEOL(); 317} 318 319void MCAsmStreamer::EndCOFFSymbolDef() { 320 OS << "\t.endef"; 321 EmitEOL(); 322} 323 324void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { 325 assert(MAI.hasDotTypeDotSizeDirective()); 326 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n'; 327} 328 329void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 330 unsigned ByteAlignment) { 331 OS << "\t.comm\t" << *Symbol << ',' << Size; 332 if (ByteAlignment != 0) { 333 if (MAI.getCOMMDirectiveAlignmentIsInBytes()) 334 OS << ',' << ByteAlignment; 335 else 336 OS << ',' << Log2_32(ByteAlignment); 337 } 338 EmitEOL(); 339} 340 341/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol. 342/// 343/// @param Symbol - The common symbol to emit. 344/// @param Size - The size of the common symbol. 345void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { 346 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!"); 347 OS << "\t.lcomm\t" << *Symbol << ',' << Size; 348 EmitEOL(); 349} 350 351void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, 352 unsigned Size, unsigned ByteAlignment) { 353 // Note: a .zerofill directive does not switch sections. 354 OS << ".zerofill "; 355 356 // This is a mach-o specific directive. 357 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section); 358 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName(); 359 360 if (Symbol != NULL) { 361 OS << ',' << *Symbol << ',' << Size; 362 if (ByteAlignment != 0) 363 OS << ',' << Log2_32(ByteAlignment); 364 } 365 EmitEOL(); 366} 367 368// .tbss sym, size, align 369// This depends that the symbol has already been mangled from the original, 370// e.g. _a. 371void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, 372 uint64_t Size, unsigned ByteAlignment) { 373 assert(Symbol != NULL && "Symbol shouldn't be NULL!"); 374 // Instead of using the Section we'll just use the shortcut. 375 // This is a mach-o specific directive and section. 376 OS << ".tbss " << *Symbol << ", " << Size; 377 378 // Output align if we have it. We default to 1 so don't bother printing 379 // that. 380 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment); 381 382 EmitEOL(); 383} 384 385static inline char toOctal(int X) { return (X&7)+'0'; } 386 387static void PrintQuotedString(StringRef Data, raw_ostream &OS) { 388 OS << '"'; 389 390 for (unsigned i = 0, e = Data.size(); i != e; ++i) { 391 unsigned char C = Data[i]; 392 if (C == '"' || C == '\\') { 393 OS << '\\' << (char)C; 394 continue; 395 } 396 397 if (isprint((unsigned char)C)) { 398 OS << (char)C; 399 continue; 400 } 401 402 switch (C) { 403 case '\b': OS << "\\b"; break; 404 case '\f': OS << "\\f"; break; 405 case '\n': OS << "\\n"; break; 406 case '\r': OS << "\\r"; break; 407 case '\t': OS << "\\t"; break; 408 default: 409 OS << '\\'; 410 OS << toOctal(C >> 6); 411 OS << toOctal(C >> 3); 412 OS << toOctal(C >> 0); 413 break; 414 } 415 } 416 417 OS << '"'; 418} 419 420 421void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { 422 assert(CurSection && "Cannot emit contents before setting section!"); 423 if (Data.empty()) return; 424 425 if (Data.size() == 1) { 426 OS << MAI.getData8bitsDirective(AddrSpace); 427 OS << (unsigned)(unsigned char)Data[0]; 428 EmitEOL(); 429 return; 430 } 431 432 // If the data ends with 0 and the target supports .asciz, use it, otherwise 433 // use .ascii 434 if (MAI.getAscizDirective() && Data.back() == 0) { 435 OS << MAI.getAscizDirective(); 436 Data = Data.substr(0, Data.size()-1); 437 } else { 438 OS << MAI.getAsciiDirective(); 439 } 440 441 OS << ' '; 442 PrintQuotedString(Data, OS); 443 EmitEOL(); 444} 445 446/// EmitIntValue - Special case of EmitValue that avoids the client having 447/// to pass in a MCExpr for constant integers. 448void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size, 449 unsigned AddrSpace) { 450 assert(CurSection && "Cannot emit contents before setting section!"); 451 const char *Directive = 0; 452 switch (Size) { 453 default: break; 454 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; 455 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; 456 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; 457 case 8: 458 Directive = MAI.getData64bitsDirective(AddrSpace); 459 // If the target doesn't support 64-bit data, emit as two 32-bit halves. 460 if (Directive) break; 461 if (isLittleEndian()) { 462 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); 463 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); 464 } else { 465 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace); 466 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace); 467 } 468 return; 469 } 470 471 assert(Directive && "Invalid size for machine code value!"); 472 OS << Directive << truncateToSize(Value, Size); 473 EmitEOL(); 474} 475 476void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size, 477 unsigned AddrSpace) { 478 assert(CurSection && "Cannot emit contents before setting section!"); 479 const char *Directive = 0; 480 switch (Size) { 481 default: break; 482 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break; 483 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break; 484 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break; 485 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break; 486 } 487 488 assert(Directive && "Invalid size for machine code value!"); 489 OS << Directive << *Value; 490 EmitEOL(); 491} 492 493void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) { 494 assert(MAI.getGPRel32Directive() != 0); 495 OS << MAI.getGPRel32Directive() << *Value; 496 EmitEOL(); 497} 498 499 500/// EmitFill - Emit NumBytes bytes worth of the value specified by 501/// FillValue. This implements directives such as '.space'. 502void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, 503 unsigned AddrSpace) { 504 if (NumBytes == 0) return; 505 506 if (AddrSpace == 0) 507 if (const char *ZeroDirective = MAI.getZeroDirective()) { 508 OS << ZeroDirective << NumBytes; 509 if (FillValue != 0) 510 OS << ',' << (int)FillValue; 511 EmitEOL(); 512 return; 513 } 514 515 // Emit a byte at a time. 516 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace); 517} 518 519void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, 520 unsigned ValueSize, 521 unsigned MaxBytesToEmit) { 522 // Some assemblers don't support non-power of two alignments, so we always 523 // emit alignments as a power of two if possible. 524 if (isPowerOf2_32(ByteAlignment)) { 525 switch (ValueSize) { 526 default: llvm_unreachable("Invalid size for machine code value!"); 527 case 1: OS << MAI.getAlignDirective(); break; 528 // FIXME: use MAI for this! 529 case 2: OS << ".p2alignw "; break; 530 case 4: OS << ".p2alignl "; break; 531 case 8: llvm_unreachable("Unsupported alignment size!"); 532 } 533 534 if (MAI.getAlignmentIsInBytes()) 535 OS << ByteAlignment; 536 else 537 OS << Log2_32(ByteAlignment); 538 539 if (Value || MaxBytesToEmit) { 540 OS << ", 0x"; 541 OS.write_hex(truncateToSize(Value, ValueSize)); 542 543 if (MaxBytesToEmit) 544 OS << ", " << MaxBytesToEmit; 545 } 546 EmitEOL(); 547 return; 548 } 549 550 // Non-power of two alignment. This is not widely supported by assemblers. 551 // FIXME: Parameterize this based on MAI. 552 switch (ValueSize) { 553 default: llvm_unreachable("Invalid size for machine code value!"); 554 case 1: OS << ".balign"; break; 555 case 2: OS << ".balignw"; break; 556 case 4: OS << ".balignl"; break; 557 case 8: llvm_unreachable("Unsupported alignment size!"); 558 } 559 560 OS << ' ' << ByteAlignment; 561 OS << ", " << truncateToSize(Value, ValueSize); 562 if (MaxBytesToEmit) 563 OS << ", " << MaxBytesToEmit; 564 EmitEOL(); 565} 566 567void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment, 568 unsigned MaxBytesToEmit) { 569 // Emit with a text fill value. 570 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(), 571 1, MaxBytesToEmit); 572} 573 574void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset, 575 unsigned char Value) { 576 // FIXME: Verify that Offset is associated with the current section. 577 OS << ".org " << *Offset << ", " << (unsigned) Value; 578 EmitEOL(); 579} 580 581 582void MCAsmStreamer::EmitFileDirective(StringRef Filename) { 583 assert(MAI.hasSingleParameterDotFile()); 584 OS << "\t.file\t"; 585 PrintQuotedString(Filename, OS); 586 EmitEOL(); 587} 588 589void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){ 590 OS << "\t.file\t" << FileNo << ' '; 591 PrintQuotedString(Filename, OS); 592 EmitEOL(); 593} 594 595void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) { 596 raw_ostream &OS = GetCommentOS(); 597 SmallString<256> Code; 598 SmallVector<MCFixup, 4> Fixups; 599 raw_svector_ostream VecOS(Code); 600 Emitter->EncodeInstruction(Inst, VecOS, Fixups); 601 VecOS.flush(); 602 603 // If we are showing fixups, create symbolic markers in the encoded 604 // representation. We do this by making a per-bit map to the fixup item index, 605 // then trying to display it as nicely as possible. 606 SmallVector<uint8_t, 64> FixupMap; 607 FixupMap.resize(Code.size() * 8); 608 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i) 609 FixupMap[i] = 0; 610 611 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 612 MCFixup &F = Fixups[i]; 613 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind()); 614 for (unsigned j = 0; j != Info.TargetSize; ++j) { 615 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j; 616 assert(Index < Code.size() * 8 && "Invalid offset in fixup!"); 617 FixupMap[Index] = 1 + i; 618 } 619 } 620 621 OS << "encoding: ["; 622 for (unsigned i = 0, e = Code.size(); i != e; ++i) { 623 if (i) 624 OS << ','; 625 626 // See if all bits are the same map entry. 627 uint8_t MapEntry = FixupMap[i * 8 + 0]; 628 for (unsigned j = 1; j != 8; ++j) { 629 if (FixupMap[i * 8 + j] == MapEntry) 630 continue; 631 632 MapEntry = uint8_t(~0U); 633 break; 634 } 635 636 if (MapEntry != uint8_t(~0U)) { 637 if (MapEntry == 0) { 638 OS << format("0x%02x", uint8_t(Code[i])); 639 } else { 640 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!"); 641 OS << char('A' + MapEntry - 1); 642 } 643 } else { 644 // Otherwise, write out in binary. 645 OS << "0b"; 646 for (unsigned j = 8; j--;) { 647 unsigned Bit = (Code[i] >> j) & 1; 648 if (uint8_t MapEntry = FixupMap[i * 8 + j]) { 649 assert(Bit == 0 && "Encoder wrote into fixed up bit!"); 650 OS << char('A' + MapEntry - 1); 651 } else 652 OS << Bit; 653 } 654 } 655 } 656 OS << "]\n"; 657 658 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 659 MCFixup &F = Fixups[i]; 660 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind()); 661 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset() 662 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n"; 663 } 664} 665 666void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { 667 assert(CurSection && "Cannot emit contents before setting section!"); 668 669 // Show the encoding in a comment if we have a code emitter. 670 if (Emitter) 671 AddEncodingComment(Inst); 672 673 // Show the MCInst if enabled. 674 if (ShowInst) { 675 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n "); 676 GetCommentOS() << "\n"; 677 } 678 679 // If we have an AsmPrinter, use that to print, otherwise print the MCInst. 680 if (InstPrinter) 681 InstPrinter->printInst(&Inst, OS); 682 else 683 Inst.print(OS, &MAI); 684 EmitEOL(); 685} 686 687/// EmitRawText - If this file is backed by a assembly streamer, this dumps 688/// the specified string in the output .s file. This capability is 689/// indicated by the hasRawTextSupport() predicate. 690void MCAsmStreamer::EmitRawText(StringRef String) { 691 if (!String.empty() && String.back() == '\n') 692 String = String.substr(0, String.size()-1); 693 OS << String; 694 EmitEOL(); 695} 696 697void MCAsmStreamer::Finish() { 698} 699 700MCStreamer *llvm::createAsmStreamer(MCContext &Context, 701 formatted_raw_ostream &OS, 702 bool isLittleEndian, 703 bool isVerboseAsm, MCInstPrinter *IP, 704 MCCodeEmitter *CE, bool ShowInst) { 705 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm, 706 IP, CE, ShowInst); 707} 708