LLVM 23.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
15#include "XtensaInstPrinter.h"
17#include "llvm/MC/MCAssembler.h"
18#include "llvm/MC/MCContext.h"
23
24using namespace llvm;
25
26static std::string getLiteralSectionName(StringRef CSectionName) {
27 std::size_t Pos = CSectionName.find(".text");
28 std::string SectionName;
29 if (Pos != std::string::npos) {
30 SectionName = CSectionName.substr(0, Pos);
31
32 if (Pos > 0)
33 SectionName += ".text";
34
35 CSectionName = CSectionName.drop_front(Pos);
36 CSectionName.consume_front(".text");
37
38 SectionName += ".literal";
39 SectionName += CSectionName;
40 } else {
41 SectionName = CSectionName;
42 SectionName += ".literal";
43 }
44 return SectionName;
45}
46
49
53
55 bool SwitchLiteralSection, SMLoc L) {
57 raw_svector_ostream LiteralStr(Str);
58
59 LiteralStr << "\t.literal " << LblSym->getName() << ", ";
60
61 if (auto CE = dyn_cast<MCConstantExpr>(Value)) {
62 LiteralStr << CE->getValue() << "\n";
63 } else if (auto SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
64 const MCSymbol &Sym = SRE->getSymbol();
66 static_cast<Xtensa::Specifier>(SRE->getSpecifier());
67 LiteralStr << Sym.getName();
68 if (Spec == Xtensa::S_TPOFF)
69 LiteralStr << "@TPOFF";
70 LiteralStr << '\n';
71 } else {
72 llvm_unreachable("unexpected constant pool entry type");
73 }
74
75 OS << LiteralStr.str();
76}
77
79 OS << "\t.literal_position\n";
80}
81
85
88
90 bool SwitchLiteralSection, SMLoc L) {
91 MCStreamer &OutStreamer = getStreamer();
92 if (SwitchLiteralSection) {
93 MCContext &Context = OutStreamer.getContext();
94 auto *CS = static_cast<MCSectionELF *>(OutStreamer.getCurrentSectionOnly());
95 std::string SectionName = getLiteralSectionName(CS->getName());
96
97 MCSection *ConstSection = Context.getELFSection(
99
100 OutStreamer.pushSection();
101 OutStreamer.switchSection(ConstSection);
102 }
103
104 OutStreamer.emitLabel(LblSym, L);
105 OutStreamer.emitValue(Value, 4, L);
106
107 if (SwitchLiteralSection) {
108 OutStreamer.popSection();
109 }
110}
111
113 MCContext &Context = getStreamer().getContext();
114
115 std::string SectionName = getLiteralSectionName(BaseSection->getName());
116
117 MCSection *ConstSection = Context.getELFSection(
119
120 ConstSection->setAlignment(Align(4));
121 MCStreamer &OutStreamer = getStreamer();
122 OutStreamer.switchSection(ConstSection);
123}
124
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:573
void setAlignment(Align Value)
Definition MCSection.h:658
StringRef getName() const
Definition MCSection.h:643
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual bool popSection()
Restore the current and previous section from the section stack.
MCContext & getContext() const
Definition MCStreamer.h:326
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:460
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:438
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:97
MCTargetStreamer(MCStreamer &S)
Represents a location in source code.
Definition SMLoc.h:22
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:629
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:290
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:655
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.
@ SHF_ALLOC
Definition ELF.h:1251
@ SHF_EXECINSTR
Definition ELF.h:1254
@ SHT_PROGBITS
Definition ELF.h:1150
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:643
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39