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