LLVM 20.0.0git
XtensaTargetStreamer.cpp
Go to the documentation of this file.
1//===-- XtensaTargetStreamer.cpp - Xtensa Target Streamer Methods ---------===//
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 provides Xtensa specific target streamer methods.
10//
11//===----------------------------------------------------------------------===//
12
14#include "XtensaInstPrinter.h"
16#include "llvm/MC/MCAssembler.h"
17#include "llvm/MC/MCContext.h"
22
23using namespace llvm;
24
25static std::string getLiteralSectionName(StringRef CSectionName) {
26 std::size_t Pos = CSectionName.find(".text");
27 std::string SectionName;
28 if (Pos != std::string::npos) {
29 SectionName = CSectionName.substr(0, Pos);
30
31 if (Pos > 0)
32 SectionName += ".text";
33
34 CSectionName = CSectionName.drop_front(Pos);
35 CSectionName.consume_front(".text");
36
37 SectionName += ".literal";
38 SectionName += CSectionName;
39 } else {
40 SectionName = CSectionName;
41 SectionName += ".literal";
42 }
43 return SectionName;
44}
45
47 : MCTargetStreamer(S) {}
48
51 : XtensaTargetStreamer(S), OS(OS) {}
52
54 bool SwitchLiteralSection, SMLoc L) {
56 raw_svector_ostream LiteralStr(Str);
57
58 LiteralStr << "\t.literal " << LblSym->getName() << ", ";
59
60 if (auto CE = dyn_cast<MCConstantExpr>(Value)) {
61 LiteralStr << CE->getValue() << "\n";
62 } else if (auto SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
63 const MCSymbol &Sym = SRE->getSymbol();
64 LiteralStr << Sym.getName() << "\n";
65 } else {
66 llvm_unreachable("unexpected constant pool entry type");
67 }
68
69 OS << LiteralStr.str();
70}
71
73 OS << "\t.literal_position\n";
74}
75
78}
79
82
84 bool SwitchLiteralSection, SMLoc L) {
85 MCStreamer &OutStreamer = getStreamer();
86 if (SwitchLiteralSection) {
87 MCContext &Context = OutStreamer.getContext();
88 auto *CS = static_cast<MCSectionELF *>(OutStreamer.getCurrentSectionOnly());
89 std::string SectionName = getLiteralSectionName(CS->getName());
90
91 MCSection *ConstSection = Context.getELFSection(
93
94 OutStreamer.pushSection();
95 OutStreamer.switchSection(ConstSection);
96 }
97
98 OutStreamer.emitLabel(LblSym, L);
99 OutStreamer.emitValue(Value, 4, L);
100
101 if (SwitchLiteralSection) {
102 OutStreamer.popSection();
103 }
104}
105
107 MCContext &Context = getStreamer().getContext();
108
109 std::string SectionName = getLiteralSectionName(BaseSection->getName());
110
111 MCSection *ConstSection = Context.getELFSection(
113
114 ConstSection->setAlignment(Align(4));
115}
116
118 return static_cast<MCELFStreamer &>(Streamer);
119}
Symbol * Sym
Definition: ELF_riscv.cpp:479
raw_pwrite_stream & OS
static std::string getLiteralSectionName(StringRef CSectionName)
Context object for machine code objects.
Definition: MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:551
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
void setAlignment(Align Value)
Definition: MCSection.h:147
StringRef getName() const
Definition: MCSection.h:130
Streaming machine code generation interface.
Definition: MCStreamer.h:213
bool popSection()
Restore the current and previous section from the section stack.
MCContext & getContext() const
Definition: MCStreamer.h:300
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:179
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:414
void pushSection()
Save the current and previous section on the section stack.
Definition: MCStreamer.h:416
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
Target specific streamer interface.
Definition: MCStreamer.h:94
MCStreamer & Streamer
Definition: MCStreamer.h:96
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:556
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:594
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:620
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:282
LLVM Value Representation.
Definition: Value.h:74
XtensaTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, bool SwitchLiteralSection, SMLoc L) override
void startLiteralSection(MCSection *Section) override
void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, bool SwitchLiteralSection, SMLoc L) override
void startLiteralSection(MCSection *Section) override
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:720
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHT_PROGBITS
Definition: ELF.h:1089
@ SHF_ALLOC
Definition: ELF.h:1186
@ SHF_EXECINSTR
Definition: ELF.h:1189
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39