LLVM 18.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"
25#include "llvm/MC/MCValue.h"
26#include "llvm/Support/LEB128.h"
28
29using namespace llvm;
30
31// This part is for ELF object output.
33 const MCSubtargetInfo &STI)
34 : RISCVTargetStreamer(S), CurrentVendor("riscv"), STI(STI) {
36 const FeatureBitset &Features = STI.getFeatureBits();
37 auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
39 MAB.getTargetOptions().getABIName()));
40}
41
43 return static_cast<RISCVELFStreamer &>(Streamer);
44}
45
54
55void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
56 getStreamer().setAttributeItem(Attribute, Value, /*OverwriteExisting=*/true);
57}
58
59void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute,
61 getStreamer().setAttributeItem(Attribute, String, /*OverwriteExisting=*/true);
62}
63
64void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
65 unsigned IntValue,
66 StringRef StringValue) {
67 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
68 /*OverwriteExisting=*/true);
69}
70
71void RISCVTargetELFStreamer::finishAttributeSection() {
73 if (S.Contents.empty())
74 return;
75
76 S.emitAttributesSection(CurrentVendor, ".riscv.attributes",
77 ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
78}
79
83 const FeatureBitset &Features = STI.getFeatureBits();
85
86 unsigned EFlags = MCA.getELFHeaderEFlags();
87
88 if (Features[RISCV::FeatureStdExtC])
89 EFlags |= ELF::EF_RISCV_RVC;
90 if (Features[RISCV::FeatureStdExtZtso])
91 EFlags |= ELF::EF_RISCV_TSO;
92
93 switch (ABI) {
96 break;
100 break;
104 break;
107 EFlags |= ELF::EF_RISCV_RVE;
108 break;
110 llvm_unreachable("Improperly initialised target ABI");
111 }
112
113 MCA.setELFHeaderEFlags(EFlags);
114}
115
116void RISCVTargetELFStreamer::reset() {
117 AttributeSection = nullptr;
118}
119
122 cast<MCSymbolELF>(Symbol).setOther(ELF::STO_RISCV_VARIANT_CC);
123}
124
125void RISCVELFStreamer::reset() {
126 static_cast<RISCVTargetStreamer *>(getTargetStreamer())->reset();
128 MappingSymbolCounter = 0;
129 LastMappingSymbols.clear();
130 LastEMS = EMS_None;
131}
132
133void RISCVELFStreamer::emitDataMappingSymbol() {
134 if (LastEMS == EMS_Data)
135 return;
136 emitMappingSymbol("$d");
137 LastEMS = EMS_Data;
138}
139
140void RISCVELFStreamer::emitInstructionsMappingSymbol() {
141 if (LastEMS == EMS_Instructions)
142 return;
143 emitMappingSymbol("$x");
144 LastEMS = EMS_Instructions;
145}
146
147void RISCVELFStreamer::emitMappingSymbol(StringRef Name) {
148 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
149 Name + "." + Twine(MappingSymbolCounter++)));
150 emitLabel(Symbol);
151 Symbol->setType(ELF::STT_NOTYPE);
152 Symbol->setBinding(ELF::STB_LOCAL);
153}
154
156 const MCExpr *Subsection) {
157 // We have to keep track of the mapping symbol state of any sections we
158 // use. Each one should start off as EMS_None, which is provided as the
159 // default constructor by DenseMap::lookup.
160 LastMappingSymbols[getPreviousSection().first] = LastEMS;
161 LastEMS = LastMappingSymbols.lookup(Section);
162
163 MCELFStreamer::changeSection(Section, Subsection);
164}
165
167 const MCSubtargetInfo &STI) {
168 emitInstructionsMappingSymbol();
170}
171
173 emitDataMappingSymbol();
175}
176
177void RISCVELFStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
178 SMLoc Loc) {
179 emitDataMappingSymbol();
180 MCELFStreamer::emitFill(NumBytes, FillValue, Loc);
181}
182
184 SMLoc Loc) {
185 emitDataMappingSymbol();
187}
188
189namespace llvm {
191 std::unique_ptr<MCAsmBackend> MAB,
192 std::unique_ptr<MCObjectWriter> MOW,
193 std::unique_ptr<MCCodeEmitter> MCE,
194 bool RelaxAll) {
196 new RISCVELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE));
197 S->getAssembler().setRelaxAll(RelaxAll);
198 return S;
199}
200} // namespace llvm
std::string Name
uint64_t Size
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
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.
Container class for subtarget features.
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:276
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:329
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:277
bool registerSymbol(const MCSymbol &Symbol)
void setRelaxAll(bool Value)
Definition: MCAssembler.h:358
Context object for machine code objects.
Definition: MCContext.h:76
SmallVector< AttributeItem, 64 > Contents
void emitAttributesSection(StringRef Vendor, const Twine &Section, unsigned Type, MCSection *&AttributeSection)
void setAttributeItems(unsigned Attribute, unsigned IntValue, StringRef StringValue, bool OverwriteExisting)
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.
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
void reset() override
state management
Definition: MCELFStreamer.h:40
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
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.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
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:41
MCStreamer & Streamer
Definition: MCStreamer.h:95
void emitDirectiveOptionPop() override
void emitDirectiveOptionPush() override
void emitDirectiveOptionNoPIC() override
void emitDirectiveOptionNoRVC() 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 setTargetABI(RISCVABI::ABI ABI)
Represents a location in source code.
Definition: SMLoc.h:23
bool empty() const
Definition: SmallVector.h:94
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
#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
@ STB_LOCAL
Definition: ELF.h:1242
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1074
@ STO_RISCV_VARIANT_CC
Definition: ELF.h:688
@ EF_RISCV_RVE
Definition: ELF.h:676
@ EF_RISCV_RVC
Definition: ELF.h:670
@ EF_RISCV_TSO
Definition: ELF.h:677
@ EF_RISCV_FLOAT_ABI_SINGLE
Definition: ELF.h:673
@ EF_RISCV_FLOAT_ABI_DOUBLE
Definition: ELF.h:674
@ STT_NOTYPE
Definition: ELF.h:1254
ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits, StringRef ABIName)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCELFStreamer * createRISCVELFStreamer(MCContext &C, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > MOW, std::unique_ptr< MCCodeEmitter > MCE, bool RelaxAll)