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