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"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FormattedStream.h"
22#include "llvm/Support/RISCVAttributes.h"
23#include "llvm/TargetParser/RISCVISAInfo.h"
24
25using namespace llvm;
26
27RISCVTargetStreamer::RISCVTargetStreamer(MCStreamer &S)
28 : MCTargetStreamer(S),
29 CLOpts(RISCV_MC::getCLOpts(Options: S.getContext().getTargetOptions())) {}
30
31void RISCVTargetStreamer::finish() { finishAttributeSection(); }
32void RISCVTargetStreamer::reset() {}
33
34void RISCVTargetStreamer::emitDirectiveOptionArch(
35 ArrayRef<RISCVOptionArchArg> Args) {}
36void RISCVTargetStreamer::emitDirectiveOptionExact() {}
37void RISCVTargetStreamer::emitDirectiveOptionNoExact() {}
38void RISCVTargetStreamer::emitDirectiveOptionPIC() {}
39void RISCVTargetStreamer::emitDirectiveOptionNoPIC() {}
40void RISCVTargetStreamer::emitDirectiveOptionPop() {}
41void RISCVTargetStreamer::emitDirectiveOptionPush() {}
42void RISCVTargetStreamer::emitDirectiveOptionRelax() {}
43void RISCVTargetStreamer::emitDirectiveOptionNoRelax() {}
44void RISCVTargetStreamer::emitDirectiveOptionRVC() {}
45void RISCVTargetStreamer::emitDirectiveOptionNoRVC() {}
46void RISCVTargetStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {}
47void RISCVTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}
48void RISCVTargetStreamer::finishAttributeSection() {}
49void RISCVTargetStreamer::emitTextAttribute(unsigned Attribute,
50 StringRef String) {}
51void RISCVTargetStreamer::emitIntTextAttribute(unsigned Attribute,
52 unsigned IntValue,
53 StringRef StringValue) {}
54
55void RISCVTargetStreamer::setTargetABI(RISCVABI::ABI ABI) {
56 assert(ABI != RISCVABI::ABI::Unknown && "Improperly initialized target ABI");
57 TargetABI = ABI;
58}
59
60void RISCVTargetStreamer::setFlagsFromFeatures(const MCSubtargetInfo &STI) {
61 HasRVC = STI.hasFeature(Feature: RISCV::FeatureStdExtZca);
62 HasTSO = STI.hasFeature(Feature: RISCV::FeatureStdExtZtso);
63}
64
65void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI,
66 bool EmitStackAlign) {
67 if (EmitStackAlign && TargetABI != RISCVABI::ABI::Unknown)
68 emitAttribute(Attribute: RISCVAttrs::STACK_ALIGN,
69 Value: RISCVABI::getStackAlignment(TargetABI).value());
70
71 auto ParseResult = RISCVFeatures::parseFeatureBits(STI);
72 if (!ParseResult) {
73 report_fatal_error(Err: ParseResult.takeError());
74 } else {
75 auto &ISAInfo = *ParseResult;
76 std::string Arch = ISAInfo->toString();
77 emitTextAttribute(Attribute: RISCVAttrs::ARCH, String: Arch);
78 setArchString(Arch);
79 }
80
81 if (CLOpts.abi_attributes && STI.hasFeature(Feature: RISCV::FeatureStdExtA)) {
82 unsigned AtomicABITag;
83 if (STI.hasFeature(Feature: RISCV::FeatureStdExtZalasr))
84 AtomicABITag = static_cast<unsigned>(RISCVAttrs::RISCVAtomicAbiTag::A7);
85 else if (STI.hasFeature(Feature: RISCV::FeatureNoTrailingSeqCstFence))
86 AtomicABITag = static_cast<unsigned>(RISCVAttrs::RISCVAtomicAbiTag::A6C);
87 else
88 AtomicABITag = static_cast<unsigned>(RISCVAttrs::RISCVAtomicAbiTag::A6S);
89 emitAttribute(Attribute: RISCVAttrs::ATOMIC_ABI, Value: AtomicABITag);
90 }
91}
92
93// This part is for ascii assembly output
94RISCVTargetAsmStreamer::RISCVTargetAsmStreamer(MCStreamer &S,
95 formatted_raw_ostream &OS)
96 : RISCVTargetStreamer(S), OS(OS) {}
97
98void RISCVTargetAsmStreamer::emitDirectiveOptionPush() {
99 OS << "\t.option\tpush\n";
100}
101
102void RISCVTargetAsmStreamer::emitDirectiveOptionPop() {
103 OS << "\t.option\tpop\n";
104}
105
106void RISCVTargetAsmStreamer::emitDirectiveOptionPIC() {
107 OS << "\t.option\tpic\n";
108}
109
110void RISCVTargetAsmStreamer::emitDirectiveOptionNoPIC() {
111 OS << "\t.option\tnopic\n";
112}
113
114void RISCVTargetAsmStreamer::emitDirectiveOptionRVC() {
115 OS << "\t.option\trvc\n";
116}
117
118void RISCVTargetAsmStreamer::emitDirectiveOptionNoRVC() {
119 OS << "\t.option\tnorvc\n";
120}
121
122void RISCVTargetAsmStreamer::emitDirectiveOptionExact() {
123 OS << "\t.option\texact\n";
124}
125
126void RISCVTargetAsmStreamer::emitDirectiveOptionNoExact() {
127 OS << "\t.option\tnoexact\n";
128}
129
130void RISCVTargetAsmStreamer::emitDirectiveOptionRelax() {
131 OS << "\t.option\trelax\n";
132}
133
134void RISCVTargetAsmStreamer::emitDirectiveOptionNoRelax() {
135 OS << "\t.option\tnorelax\n";
136}
137
138void RISCVTargetAsmStreamer::emitDirectiveOptionArch(
139 ArrayRef<RISCVOptionArchArg> Args) {
140 OS << "\t.option\tarch";
141 for (const auto &Arg : Args) {
142 OS << ", ";
143 switch (Arg.Type) {
144 case RISCVOptionArchArgType::Full:
145 break;
146 case RISCVOptionArchArgType::Plus:
147 OS << "+";
148 break;
149 case RISCVOptionArchArgType::Minus:
150 OS << "-";
151 break;
152 }
153 OS << Arg.Value;
154 }
155 OS << "\n";
156}
157
158void RISCVTargetAsmStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
159 OS << "\t.variant_cc\t" << Symbol.getName() << "\n";
160}
161
162void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
163 OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n";
164}
165
166void RISCVTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
167 StringRef String) {
168 OS << "\t.attribute\t" << Attribute << ", \"" << String << "\"\n";
169}
170
171void RISCVTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
172 unsigned IntValue,
173 StringRef StringValue) {}
174
175void RISCVTargetAsmStreamer::finishAttributeSection() {}
176