LLVM 19.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
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
LLVMContext & Context
raw_pwrite_stream & OS
static std::string getLiteralSectionName(StringRef CSectionName)
Context object for machine code objects.
Definition: MCContext.h:81
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:26
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
void setAlignment(Align Value)
Definition: MCSection.h:141
StringRef getName() const
Definition: MCSection.h:124
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCContext & getContext() const
Definition: MCStreamer.h:297
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:180
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:424
bool popSection()
Restore the current and previous section from the section stack.
Definition: MCStreamer.h:424
void pushSection()
Save the current and previous section on the section stack.
Definition: MCStreamer.h:415
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
Target specific streamer interface.
Definition: MCStreamer.h:93
MCStreamer & Streamer
Definition: MCStreamer.h:95
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:563
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:601
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:627
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:289
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:690
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:715
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_EXECINSTR
Definition: ELF.h:1160
@ SHT_PROGBITS
Definition: ELF.h:1063
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