NVPTXTargetStreamer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. void NVPTXTargetStreamer::outputDwarfFileDirectives() {
  23. for (const std::string &S : DwarfFiles)
  24. getStreamer().emitRawText(S);
  25. DwarfFiles.clear();
  26. }
  27. void NVPTXTargetStreamer::closeLastSection() {
  28. if (HasSections)
  29. getStreamer().emitRawText("\t}");
  30. }
  31. void NVPTXTargetStreamer::emitDwarfFileDirective(StringRef Directive) {
  32. DwarfFiles.emplace_back(Directive);
  33. }
  34. static bool isDwarfSection(const MCObjectFileInfo *FI,
  35. const MCSection *Section) {
  36. // FIXME: the checks for the DWARF sections are very fragile and should be
  37. // fixed up in a followup patch.
  38. if (!Section || Section->getKind().isText() ||
  39. Section->getKind().isWriteable())
  40. return false;
  41. return Section == FI->getDwarfAbbrevSection() ||
  42. Section == FI->getDwarfInfoSection() ||
  43. Section == FI->getDwarfMacinfoSection() ||
  44. Section == FI->getDwarfFrameSection() ||
  45. Section == FI->getDwarfAddrSection() ||
  46. Section == FI->getDwarfRangesSection() ||
  47. Section == FI->getDwarfARangesSection() ||
  48. Section == FI->getDwarfLocSection() ||
  49. Section == FI->getDwarfStrSection() ||
  50. Section == FI->getDwarfLineSection() ||
  51. Section == FI->getDwarfStrOffSection() ||
  52. Section == FI->getDwarfLineStrSection() ||
  53. Section == FI->getDwarfPubNamesSection() ||
  54. Section == FI->getDwarfPubTypesSection() ||
  55. Section == FI->getDwarfSwiftASTSection() ||
  56. Section == FI->getDwarfTypesDWOSection() ||
  57. Section == FI->getDwarfAbbrevDWOSection() ||
  58. Section == FI->getDwarfAccelObjCSection() ||
  59. Section == FI->getDwarfAccelNamesSection() ||
  60. Section == FI->getDwarfAccelTypesSection() ||
  61. Section == FI->getDwarfAccelNamespaceSection() ||
  62. Section == FI->getDwarfLocDWOSection() ||
  63. Section == FI->getDwarfStrDWOSection() ||
  64. Section == FI->getDwarfCUIndexSection() ||
  65. Section == FI->getDwarfInfoDWOSection() ||
  66. Section == FI->getDwarfLineDWOSection() ||
  67. Section == FI->getDwarfTUIndexSection() ||
  68. Section == FI->getDwarfStrOffDWOSection() ||
  69. Section == FI->getDwarfDebugNamesSection() ||
  70. Section == FI->getDwarfDebugInlineSection() ||
  71. Section == FI->getDwarfGnuPubNamesSection() ||
  72. Section == FI->getDwarfGnuPubTypesSection();
  73. }
  74. void NVPTXTargetStreamer::changeSection(const MCSection *CurSection,
  75. MCSection *Section,
  76. const MCExpr *SubSection,
  77. raw_ostream &OS) {
  78. assert(!SubSection && "SubSection is not null!");
  79. const MCObjectFileInfo *FI = getStreamer().getContext().getObjectFileInfo();
  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. outputDwarfFileDirectives();
  86. OS << "\t.section";
  87. Section->PrintSwitchToSection(*getStreamer().getContext().getAsmInfo(),
  88. getStreamer().getContext().getTargetTriple(),
  89. OS, SubSection);
  90. // DWARF sections are enclosed into braces - emit the open one.
  91. OS << "\t{\n";
  92. HasSections = true;
  93. }
  94. }
  95. void NVPTXTargetStreamer::emitRawBytes(StringRef Data) {
  96. MCTargetStreamer::emitRawBytes(Data);
  97. // TODO: enable this once the bug in the ptxas with the packed bytes is
  98. // resolved. Currently, (it is confirmed by NVidia) it causes a crash in
  99. // ptxas.
  100. #if 0
  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. const char *Label = Directive;
  112. for (auto It = std::next(Data.bytes_begin(), I * MaxLen),
  113. End = (I == NumChunks - 1)
  114. ? Data.bytes_end()
  115. : std::next(Data.bytes_begin(), (I + 1) * MaxLen);
  116. It != End; ++It) {
  117. OS << Label << (unsigned)*It;
  118. if (Label == Directive)
  119. Label = ",";
  120. }
  121. Streamer.emitRawText(OS.str());
  122. }
  123. #endif
  124. }