NVPTXTargetStreamer.cpp 5.4 KB

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