1 //=====- NVPTXTargetStreamer.cpp - NVPTXTargetStreamer class ------------=====// 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 implements the NVPTXTargetStreamer class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "NVPTXTargetStreamer.h" 15 #include "llvm/MC/MCAsmInfo.h" 16 #include "llvm/MC/MCContext.h" 17 #include "llvm/MC/MCObjectFileInfo.h" 18 19 using namespace llvm; 20 21 // 22 // NVPTXTargetStreamer Implemenation 23 // 24 NVPTXTargetStreamer::NVPTXTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {} 25 26 NVPTXTargetStreamer::~NVPTXTargetStreamer() = default; 27 28 void NVPTXTargetStreamer::emitDwarfFileDirective(StringRef Directive) { 29 DwarfFiles.emplace_back(Directive); 30 } 31 32 static bool isDwarfSection(const MCObjectFileInfo *FI, 33 const MCSection *Section) { 34 // FIXME: the checks for the DWARF sections are very fragile and should be 35 // fixed up in a followup patch. 36 if (!Section || Section->getKind().isText() || 37 Section->getKind().isWriteable()) 38 return false; 39 return Section == FI->getDwarfAbbrevSection() || 40 Section == FI->getDwarfInfoSection() || 41 Section == FI->getDwarfMacinfoSection() || 42 Section == FI->getDwarfFrameSection() || 43 Section == FI->getDwarfAddrSection() || 44 Section == FI->getDwarfRangesSection() || 45 Section == FI->getDwarfARangesSection() || 46 Section == FI->getDwarfLocSection() || 47 Section == FI->getDwarfStrSection() || 48 Section == FI->getDwarfLineSection() || 49 Section == FI->getDwarfStrOffSection() || 50 Section == FI->getDwarfLineStrSection() || 51 Section == FI->getDwarfPubNamesSection() || 52 Section == FI->getDwarfPubTypesSection() || 53 Section == FI->getDwarfSwiftASTSection() || 54 Section == FI->getDwarfTypesDWOSection() || 55 Section == FI->getDwarfAbbrevDWOSection() || 56 Section == FI->getDwarfAccelObjCSection() || 57 Section == FI->getDwarfAccelNamesSection() || 58 Section == FI->getDwarfAccelTypesSection() || 59 Section == FI->getDwarfAccelNamespaceSection() || 60 Section == FI->getDwarfLocDWOSection() || 61 Section == FI->getDwarfStrDWOSection() || 62 Section == FI->getDwarfCUIndexSection() || 63 Section == FI->getDwarfInfoDWOSection() || 64 Section == FI->getDwarfLineDWOSection() || 65 Section == FI->getDwarfTUIndexSection() || 66 Section == FI->getDwarfStrOffDWOSection() || 67 Section == FI->getDwarfDebugNamesSection() || 68 Section == FI->getDwarfDebugInlineSection() || 69 Section == FI->getDwarfGnuPubNamesSection() || 70 Section == FI->getDwarfGnuPubTypesSection(); 71 } 72 73 void NVPTXTargetStreamer::changeSection(const MCSection *CurSection, 74 MCSection *Section, 75 const MCExpr *SubSection, 76 raw_ostream &OS) { 77 assert(!SubSection && "SubSection is not null!"); 78 const MCObjectFileInfo *FI = getStreamer().getContext().getObjectFileInfo(); 79 // FIXME: remove comment once debug info is properly supported. 80 // Emit closing brace for DWARF sections only. 81 if (isDwarfSection(FI, CurSection)) 82 OS << "//\t}\n"; 83 if (isDwarfSection(FI, Section)) { 84 // Emit DWARF .file directives in the outermost scope. 85 for (const std::string &S : DwarfFiles) 86 getStreamer().EmitRawText(S.data()); 87 DwarfFiles.clear(); 88 OS << "//\t.section"; 89 Section->PrintSwitchToSection(*getStreamer().getContext().getAsmInfo(), 90 FI->getTargetTriple(), OS, SubSection); 91 // DWARF sections are enclosed into braces - emit the open one. 92 OS << "//\t{\n"; 93 } 94 } 95 96 void NVPTXTargetStreamer::emitRawBytes(StringRef Data) { 97 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 98 const char *Directive = MAI->getData8bitsDirective(); 99 unsigned NumElements = Data.size(); 100 const unsigned MaxLen = 40; 101 unsigned NumChunks = 1 + ((NumElements - 1) / MaxLen); 102 // Split the very long directives into several parts if the limit is 103 // specified. 104 for (unsigned I = 0; I < NumChunks; ++I) { 105 SmallString<128> Str; 106 raw_svector_ostream OS(Str); 107 108 const char *Label = Directive; 109 for (auto It = std::next(Data.bytes_begin(), I * MaxLen), 110 End = (I == NumChunks - 1) 111 ? Data.bytes_end() 112 : std::next(Data.bytes_begin(), (I + 1) * MaxLen); 113 It != End; ++It) { 114 OS << Label << (unsigned)*It; 115 if (Label == Directive) 116 Label = ","; 117 } 118 Streamer.EmitRawText(OS.str()); 119 } 120 } 121 122