Path: blob/main/contrib/llvm-project/llvm/lib/Target/Xtensa/MCTargetDesc/XtensaTargetStreamer.cpp
35295 views
//===-- XtensaTargetStreamer.cpp - Xtensa Target Streamer Methods ---------===//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 provides Xtensa specific target streamer methods.9//10//===----------------------------------------------------------------------===//1112#include "XtensaTargetStreamer.h"13#include "XtensaInstPrinter.h"14#include "llvm/BinaryFormat/ELF.h"15#include "llvm/MC/MCAssembler.h"16#include "llvm/MC/MCContext.h"17#include "llvm/MC/MCObjectFileInfo.h"18#include "llvm/MC/MCSectionELF.h"19#include "llvm/Support/Casting.h"20#include "llvm/Support/FormattedStream.h"2122using namespace llvm;2324static std::string getLiteralSectionName(StringRef CSectionName) {25std::size_t Pos = CSectionName.find(".text");26std::string SectionName;27if (Pos != std::string::npos) {28SectionName = CSectionName.substr(0, Pos);2930if (Pos > 0)31SectionName += ".text";3233CSectionName = CSectionName.drop_front(Pos);34CSectionName.consume_front(".text");3536SectionName += ".literal";37SectionName += CSectionName;38} else {39SectionName = CSectionName;40SectionName += ".literal";41}42return SectionName;43}4445XtensaTargetStreamer::XtensaTargetStreamer(MCStreamer &S)46: MCTargetStreamer(S) {}4748XtensaTargetAsmStreamer::XtensaTargetAsmStreamer(MCStreamer &S,49formatted_raw_ostream &OS)50: XtensaTargetStreamer(S), OS(OS) {}5152void XtensaTargetAsmStreamer::emitLiteral(MCSymbol *LblSym, const MCExpr *Value,53bool SwitchLiteralSection, SMLoc L) {54SmallString<60> Str;55raw_svector_ostream LiteralStr(Str);5657LiteralStr << "\t.literal " << LblSym->getName() << ", ";5859if (auto CE = dyn_cast<MCConstantExpr>(Value)) {60LiteralStr << CE->getValue() << "\n";61} else if (auto SRE = dyn_cast<MCSymbolRefExpr>(Value)) {62const MCSymbol &Sym = SRE->getSymbol();63LiteralStr << Sym.getName() << "\n";64} else {65llvm_unreachable("unexpected constant pool entry type");66}6768OS << LiteralStr.str();69}7071void XtensaTargetAsmStreamer::emitLiteralPosition() {72OS << "\t.literal_position\n";73}7475void XtensaTargetAsmStreamer::startLiteralSection(MCSection *BaseSection) {76emitLiteralPosition();77}7879XtensaTargetELFStreamer::XtensaTargetELFStreamer(MCStreamer &S)80: XtensaTargetStreamer(S) {}8182void XtensaTargetELFStreamer::emitLiteral(MCSymbol *LblSym, const MCExpr *Value,83bool SwitchLiteralSection, SMLoc L) {84MCStreamer &OutStreamer = getStreamer();85if (SwitchLiteralSection) {86MCContext &Context = OutStreamer.getContext();87auto *CS = static_cast<MCSectionELF *>(OutStreamer.getCurrentSectionOnly());88std::string SectionName = getLiteralSectionName(CS->getName());8990MCSection *ConstSection = Context.getELFSection(91SectionName, ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);9293OutStreamer.pushSection();94OutStreamer.switchSection(ConstSection);95}9697OutStreamer.emitLabel(LblSym, L);98OutStreamer.emitValue(Value, 4, L);99100if (SwitchLiteralSection) {101OutStreamer.popSection();102}103}104105void XtensaTargetELFStreamer::startLiteralSection(MCSection *BaseSection) {106MCContext &Context = getStreamer().getContext();107108std::string SectionName = getLiteralSectionName(BaseSection->getName());109110MCSection *ConstSection = Context.getELFSection(111SectionName, ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);112113ConstSection->setAlignment(Align(4));114}115116MCELFStreamer &XtensaTargetELFStreamer::getStreamer() {117return static_cast<MCELFStreamer &>(Streamer);118}119120121