LLVM 22.0.0git
RISCVTargetStreamer.cpp
Go to the documentation of this file.
1//===-- RISCVTargetStreamer.cpp - RISC-V 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 "RISCVTargetStreamer.h"
14#include "RISCVBaseInfo.h"
15#include "RISCVMCTargetDesc.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCSymbol.h"
28
29using namespace llvm;
30
31// This option controls whether or not we emit ELF attributes for ABI features,
32// like RISC-V atomics or X3 usage.
34 "riscv-abi-attributes",
35 cl::desc("Enable emitting RISC-V ELF attributes for ABI features"),
37
39
42
61 unsigned IntValue,
62 StringRef StringValue) {}
63
65 const uint32_t Feature1And) {
66 MCStreamer &OutStreamer = getStreamer();
67 MCContext &Ctx = OutStreamer.getContext();
68
69 const Triple &Triple = Ctx.getTargetTriple();
70 Align NoteAlign;
71 uint64_t DescSize;
72 if (Triple.isArch64Bit()) {
73 NoteAlign = Align(8);
74 DescSize = 16;
75 } else {
77 NoteAlign = Align(4);
78 DescSize = 12;
79 }
80
82 MCSection *const NoteSection =
83 Ctx.getELFSection(".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
84 OutStreamer.pushSection();
85 OutStreamer.switchSection(NoteSection);
86
87 // Emit the note header
88 OutStreamer.emitValueToAlignment(NoteAlign);
89 OutStreamer.emitIntValue(4, 4); // n_namsz
90 OutStreamer.emitIntValue(DescSize, 4); // n_descsz
91 OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); // n_type
92 OutStreamer.emitBytes(StringRef("GNU", 4)); // n_name
93
94 // Emit n_desc field
95
96 // Emit the feature_1_and property
97 OutStreamer.emitIntValue(ELF::GNU_PROPERTY_RISCV_FEATURE_1_AND, 4); // pr_type
98 OutStreamer.emitIntValue(4, 4); // pr_datasz
99 OutStreamer.emitIntValue(Feature1And, 4); // pr_data
100 OutStreamer.emitValueToAlignment(NoteAlign); // pr_padding
101
102 OutStreamer.popSection();
103}
104
106 assert(ABI != RISCVABI::ABI_Unknown && "Improperly initialized target ABI");
107 TargetABI = ABI;
108}
109
111 HasRVC = STI.hasFeature(RISCV::FeatureStdExtZca);
112 HasTSO = STI.hasFeature(RISCV::FeatureStdExtZtso);
113}
114
116 bool EmitStackAlign) {
117 if (EmitStackAlign) {
118 unsigned StackAlign;
119 if (TargetABI == RISCVABI::ABI_ILP32E)
120 StackAlign = 4;
121 else if (TargetABI == RISCVABI::ABI_LP64E)
122 StackAlign = 8;
123 else
124 StackAlign = 16;
126 }
127
129 STI.hasFeature(RISCV::Feature64Bit), STI.getFeatureBits());
130 if (!ParseResult) {
131 report_fatal_error(ParseResult.takeError());
132 } else {
133 auto &ISAInfo = *ParseResult;
134 emitTextAttribute(RISCVAttrs::ARCH, ISAInfo->toString());
135 }
136
137 if (RiscvAbiAttr && STI.hasFeature(RISCV::FeatureStdExtA)) {
138 unsigned AtomicABITag;
139 if (STI.hasFeature(RISCV::FeatureStdExtZalasr))
140 AtomicABITag = static_cast<unsigned>(RISCVAttrs::RISCVAtomicAbiTag::A7);
141 else if (STI.hasFeature(RISCV::FeatureNoTrailingSeqCstFence))
142 AtomicABITag = static_cast<unsigned>(RISCVAttrs::RISCVAtomicAbiTag::A6C);
143 else
144 AtomicABITag = static_cast<unsigned>(RISCVAttrs::RISCVAtomicAbiTag::A6S);
146 }
147}
148
149// This part is for ascii assembly output
152 : RISCVTargetStreamer(S), OS(OS) {}
153
155 OS << "\t.option\tpush\n";
156}
157
159 OS << "\t.option\tpop\n";
160}
161
163 OS << "\t.option\tpic\n";
164}
165
167 OS << "\t.option\tnopic\n";
168}
169
171 OS << "\t.option\trvc\n";
172}
173
175 OS << "\t.option\tnorvc\n";
176}
177
179 OS << "\t.option\texact\n";
180}
181
183 OS << "\t.option\tnoexact\n";
184}
185
187 OS << "\t.option\trelax\n";
188}
189
191 OS << "\t.option\tnorelax\n";
192}
193
196 OS << "\t.option\tarch";
197 for (const auto &Arg : Args) {
198 OS << ", ";
199 switch (Arg.Type) {
201 break;
203 OS << "+";
204 break;
206 OS << "-";
207 break;
208 }
209 OS << Arg.Value;
210 }
211 OS << "\n";
212}
213
215 OS << "\t.variant_cc\t" << Symbol.getName() << "\n";
216}
217
218void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
219 OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n";
220}
221
222void RISCVTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
224 OS << "\t.attribute\t" << Attribute << ", \"" << String << "\"\n";
225}
226
227void RISCVTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
228 unsigned IntValue,
229 StringRef StringValue) {}
230
231void RISCVTargetAsmStreamer::finishAttributeSection() {}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > RiscvAbiAttr("riscv-abi-attributes", cl::desc("Enable emitting RISC-V ELF attributes for ABI features"), cl::Hidden)
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Context object for machine code objects.
Definition: MCContext.h:83
Environment getObjectFileType() const
Definition: MCContext.h:392
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:549
const Triple & getTargetTriple() const
Definition: MCContext.h:400
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
virtual bool popSection()
Restore the current and previous section from the section stack.
MCContext & getContext() const
Definition: MCStreamer.h:314
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
Definition: MCStreamer.cpp:132
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.
virtual void emitBytes(StringRef Data)
Emit the bytes in Data into the output.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) 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
Target specific streamer interface.
Definition: MCStreamer.h:93
MCStreamer & getStreamer()
Definition: MCStreamer.h:101
This class represents success/failure for parsing-like operations that find it important to chain tog...
void emitDirectiveOptionArch(ArrayRef< RISCVOptionArchArg > Args) override
RISCVTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
void emitDirectiveVariantCC(MCSymbol &Symbol) override
void emitNoteGnuPropertySection(const uint32_t Feature1And)
virtual void emitDirectiveOptionNoRelax()
virtual void emitAttribute(unsigned Attribute, unsigned Value)
virtual void emitTextAttribute(unsigned Attribute, StringRef String)
virtual void emitDirectiveVariantCC(MCSymbol &Symbol)
void emitTargetAttributes(const MCSubtargetInfo &STI, bool EmitStackAlign)
void setFlagsFromFeatures(const MCSubtargetInfo &STI)
virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, StringRef StringValue)
virtual void emitDirectiveOptionNoExact()
void setTargetABI(RISCVABI::ABI ABI)
virtual void emitDirectiveOptionArch(ArrayRef< RISCVOptionArchArg > Args)
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_ABI bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1771
LLVM_ABI bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition: Triple.cpp:1775
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
LLVM Value Representation.
Definition: Value.h:75
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
@ NT_GNU_PROPERTY_TYPE_0
Definition: ELF.h:1801
@ GNU_PROPERTY_RISCV_FEATURE_1_AND
Definition: ELF.h:1837
@ SHT_NOTE
Definition: ELF.h:1146
@ SHF_ALLOC
Definition: ELF.h:1240
llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39