1 //===-- RISCVTargetStreamer.cpp - RISCV 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 RISCV specific target streamer methods.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVTargetStreamer.h"
14 #include "RISCVBaseInfo.h"
15 #include "RISCVMCTargetDesc.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/Support/FormattedStream.h"
18 #include "llvm/Support/RISCVAttributes.h"
19 #include "llvm/Support/RISCVISAInfo.h"
20 
21 using namespace llvm;
22 
23 RISCVTargetStreamer::RISCVTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
24 
25 void RISCVTargetStreamer::finish() { finishAttributeSection(); }
26 void RISCVTargetStreamer::reset() {}
27 
28 void RISCVTargetStreamer::emitDirectiveOptionPush() {}
29 void RISCVTargetStreamer::emitDirectiveOptionPop() {}
30 void RISCVTargetStreamer::emitDirectiveOptionPIC() {}
31 void RISCVTargetStreamer::emitDirectiveOptionNoPIC() {}
32 void RISCVTargetStreamer::emitDirectiveOptionRVC() {}
33 void RISCVTargetStreamer::emitDirectiveOptionNoRVC() {}
34 void RISCVTargetStreamer::emitDirectiveOptionRelax() {}
35 void RISCVTargetStreamer::emitDirectiveOptionNoRelax() {}
36 void RISCVTargetStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {}
37 void RISCVTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}
38 void RISCVTargetStreamer::finishAttributeSection() {}
39 void RISCVTargetStreamer::emitTextAttribute(unsigned Attribute,
40                                             StringRef String) {}
41 void RISCVTargetStreamer::emitIntTextAttribute(unsigned Attribute,
42                                                unsigned IntValue,
43                                                StringRef StringValue) {}
44 void RISCVTargetStreamer::setTargetABI(RISCVABI::ABI ABI) {
45   assert(ABI != RISCVABI::ABI_Unknown && "Improperly initialized target ABI");
46   TargetABI = ABI;
47 }
48 
49 void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {
50   if (STI.hasFeature(RISCV::FeatureRV32E))
51     emitAttribute(RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_4);
52   else
53     emitAttribute(RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_16);
54 
55   auto ParseResult = RISCVFeatures::parseFeatureBits(
56       STI.hasFeature(RISCV::Feature64Bit), STI.getFeatureBits());
57   if (!ParseResult) {
58     report_fatal_error(ParseResult.takeError());
59   } else {
60     auto &ISAInfo = *ParseResult;
61     emitTextAttribute(RISCVAttrs::ARCH, ISAInfo->toString());
62   }
63 }
64 
65 // This part is for ascii assembly output
66 RISCVTargetAsmStreamer::RISCVTargetAsmStreamer(MCStreamer &S,
67                                                formatted_raw_ostream &OS)
68     : RISCVTargetStreamer(S), OS(OS) {}
69 
70 void RISCVTargetAsmStreamer::emitDirectiveOptionPush() {
71   OS << "\t.option\tpush\n";
72 }
73 
74 void RISCVTargetAsmStreamer::emitDirectiveOptionPop() {
75   OS << "\t.option\tpop\n";
76 }
77 
78 void RISCVTargetAsmStreamer::emitDirectiveOptionPIC() {
79   OS << "\t.option\tpic\n";
80 }
81 
82 void RISCVTargetAsmStreamer::emitDirectiveOptionNoPIC() {
83   OS << "\t.option\tnopic\n";
84 }
85 
86 void RISCVTargetAsmStreamer::emitDirectiveOptionRVC() {
87   OS << "\t.option\trvc\n";
88 }
89 
90 void RISCVTargetAsmStreamer::emitDirectiveOptionNoRVC() {
91   OS << "\t.option\tnorvc\n";
92 }
93 
94 void RISCVTargetAsmStreamer::emitDirectiveOptionRelax() {
95   OS << "\t.option\trelax\n";
96 }
97 
98 void RISCVTargetAsmStreamer::emitDirectiveOptionNoRelax() {
99   OS << "\t.option\tnorelax\n";
100 }
101 
102 void RISCVTargetAsmStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
103   OS << "\t.variant_cc\t" << Symbol.getName() << "\n";
104 }
105 
106 void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
107   OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n";
108 }
109 
110 void RISCVTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
111                                                StringRef String) {
112   OS << "\t.attribute\t" << Attribute << ", \"" << String << "\"\n";
113 }
114 
115 void RISCVTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
116                                                   unsigned IntValue,
117                                                   StringRef StringValue) {}
118 
119 void RISCVTargetAsmStreamer::finishAttributeSection() {}
120