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