LLVM 22.0.0git
RISCVELFStreamer.cpp
Go to the documentation of this file.
1//===-- RISCVELFStreamer.cpp - RISC-V ELF 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 RISC-V specific target streamer methods.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVELFStreamer.h"
14#include "RISCVAsmBackend.h"
15#include "RISCVBaseInfo.h"
16#include "RISCVMCTargetDesc.h"
19#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCContext.h"
24
25using namespace llvm;
26
27// This part is for ELF object output.
29 const MCSubtargetInfo &STI)
30 : RISCVTargetStreamer(S), CurrentVendor("riscv") {
32 const FeatureBitset &Features = STI.getFeatureBits();
33 auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
35 MAB.getTargetOptions().getABIName()));
37}
38
40 std::unique_ptr<MCAsmBackend> MAB,
41 std::unique_ptr<MCObjectWriter> MOW,
42 std::unique_ptr<MCCodeEmitter> MCE)
43 : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {}
44
46 return static_cast<RISCVELFStreamer &>(Streamer);
47}
48
59
60void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
61 getStreamer().setAttributeItem(Attribute, Value, /*OverwriteExisting=*/true);
62}
63
64void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute,
66 getStreamer().setAttributeItem(Attribute, String, /*OverwriteExisting=*/true);
67}
68
69void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
70 unsigned IntValue,
71 StringRef StringValue) {
72 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
73 /*OverwriteExisting=*/true);
74}
75
76void RISCVTargetELFStreamer::finishAttributeSection() {
78 if (S.Contents.empty())
79 return;
80
81 S.emitAttributesSection(CurrentVendor, ".riscv.attributes",
82 ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
83}
84
89
90 unsigned EFlags = W.getELFHeaderEFlags();
91
92 if (hasRVC())
93 EFlags |= ELF::EF_RISCV_RVC;
94 if (hasTSO())
95 EFlags |= ELF::EF_RISCV_TSO;
96
97 switch (ABI) {
100 break;
104 break;
108 break;
111 EFlags |= ELF::EF_RISCV_RVE;
112 break;
114 llvm_unreachable("Improperly initialised target ABI");
115 }
116
117 W.setELFHeaderEFlags(EFlags);
118}
119
120void RISCVTargetELFStreamer::reset() {
121 AttributeSection = nullptr;
122}
123
126 static_cast<MCSymbolELF &>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
127}
128
129void RISCVELFStreamer::reset() {
130 static_cast<RISCVTargetStreamer *>(getTargetStreamer())->reset();
132 LastMappingSymbols.clear();
133 LastEMS = EMS_None;
134}
135
136void RISCVELFStreamer::emitDataMappingSymbol() {
137 if (LastEMS == EMS_Data)
138 return;
139 emitMappingSymbol("$d");
140 LastEMS = EMS_Data;
141}
142
143void RISCVELFStreamer::emitInstructionsMappingSymbol() {
144 if (LastEMS == EMS_Instructions)
145 return;
146 emitMappingSymbol("$x");
147 LastEMS = EMS_Instructions;
148}
149
150void RISCVELFStreamer::emitMappingSymbol(StringRef Name) {
151 auto *Symbol =
152 static_cast<MCSymbolELF *>(getContext().createLocalSymbol(Name));
153 emitLabel(Symbol);
154 Symbol->setType(ELF::STT_NOTYPE);
155 Symbol->setBinding(ELF::STB_LOCAL);
156}
157
159 // We have to keep track of the mapping symbol state of any sections we
160 // use. Each one should start off as EMS_None, which is provided as the
161 // default constructor by DenseMap::lookup.
162 LastMappingSymbols[getPreviousSection().first] = LastEMS;
163 LastEMS = LastMappingSymbols.lookup(Section);
164
165 MCELFStreamer::changeSection(Section, Subsection);
166}
167
169 const MCSubtargetInfo &STI) {
170 emitInstructionsMappingSymbol();
172}
173
175 emitDataMappingSymbol();
177}
178
179void RISCVELFStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
180 SMLoc Loc) {
181 emitDataMappingSymbol();
182 MCELFStreamer::emitFill(NumBytes, FillValue, Loc);
183}
184
186 SMLoc Loc) {
187 emitDataMappingSymbol();
189}
190
192 std::unique_ptr<MCAsmBackend> &&MAB,
193 std::unique_ptr<MCObjectWriter> &&MOW,
194 std::unique_ptr<MCCodeEmitter> &&MCE) {
195 return new RISCVELFStreamer(C, std::move(MAB), std::move(MOW),
196 std::move(MCE));
197}
std::string Name
uint64_t Size
Container class for subtarget features.
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:175
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
Context object for machine code objects.
Definition: MCContext.h:83
LLVM_ABI MCSymbol * createLocalSymbol(StringRef Name)
Create a local, non-temporary symbol like an ELF mapping symbol.
Definition: MCContext.cpp:392
SmallVector< AttributeItem, 64 > Contents
void emitAttributesSection(StringRef Vendor, const Twine &Section, unsigned Type, MCSection *&AttributeSection)
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void setAttributeItems(unsigned Attribute, unsigned IntValue, StringRef StringValue, bool OverwriteExisting)
ELFObjectWriter & getWriter()
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
void reset() override
state management
Definition: MCELFStreamer.h:40
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
MCAssembler & getAssembler()
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCSectionSubPair getPreviousSection() const
Return the previous section that the streamer is emitting code to.
Definition: MCStreamer.h:426
MCContext & getContext() const
Definition: MCStreamer.h:314
MCTargetStreamer * getTargetStreamer()
Definition: MCStreamer.h:324
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
const FeatureBitset & getFeatureBits() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MCStreamer & Streamer
Definition: MCStreamer.h:95
void changeSection(MCSection *Section, uint32_t Subsection) override
This is called by popSection and switchSection, if the current section changes.
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
RISCVELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE)
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc) override
Emit Size bytes worth of the value specified by FillValue.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void emitDirectiveOptionExact() override
void emitDirectiveOptionPop() override
void emitDirectiveOptionPush() override
void emitDirectiveOptionNoPIC() override
void emitDirectiveOptionNoRVC() override
void emitDirectiveOptionNoExact() override
RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
void emitDirectiveVariantCC(MCSymbol &Symbol) override
void emitDirectiveOptionRelax() override
RISCVELFStreamer & getStreamer()
void emitDirectiveOptionNoRelax() override
void emitDirectiveOptionPIC() override
void emitDirectiveOptionRVC() override
RISCVABI::ABI getTargetABI() const
void setFlagsFromFeatures(const MCSubtargetInfo &STI)
void setTargetABI(RISCVABI::ABI ABI)
Represents a location in source code.
Definition: SMLoc.h:23
bool empty() const
Definition: SmallVector.h:82
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
LLVM Value Representation.
Definition: Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ STO_RISCV_VARIANT_CC
Definition: ELF.h:729
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1223
@ EF_RISCV_RVE
Definition: ELF.h:714
@ EF_RISCV_RVC
Definition: ELF.h:708
@ EF_RISCV_TSO
Definition: ELF.h:715
@ EF_RISCV_FLOAT_ABI_SINGLE
Definition: ELF.h:711
@ EF_RISCV_FLOAT_ABI_DOUBLE
Definition: ELF.h:712
@ STB_LOCAL
Definition: ELF.h:1396
@ STT_NOTYPE
Definition: ELF.h:1408
ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits, StringRef ABIName)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCStreamer * createRISCVELFStreamer(const Triple &, MCContext &C, std::unique_ptr< MCAsmBackend > &&MAB, std::unique_ptr< MCObjectWriter > &&MOW, std::unique_ptr< MCCodeEmitter > &&MCE)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856