LLVM 22.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
48
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
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
static std::string getLiteralSectionName(StringRef CSectionName)
Context object for machine code objects.
Definition MCContext.h:83
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.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:496
void setAlignment(Align Value)
Definition MCSection.h:580
StringRef getName() const
Definition MCSection.h:565
Streaming machine code generation interface.
Definition MCStreamer.h:220
virtual bool popSection()
Restore the current and previous section from the section stack.
MCContext & getContext() const
Definition MCStreamer.h:314
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
void pushSection()
Save the current and previous section on the section stack.
Definition MCStreamer.h:443
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:421
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
MCStreamer & Streamer
Definition MCStreamer.h:95
MCTargetStreamer(MCStreamer &S)
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:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:581
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:619
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:645
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:301
LLVM Value Representation.
Definition Value.h:75
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.
StringRef str() const
Return a StringRef for the vector contents.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHT_PROGBITS
Definition ELF.h:1140
@ SHF_ALLOC
Definition ELF.h:1240
@ SHF_EXECINSTR
Definition ELF.h:1243
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39