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/Support/FormattedStream.h"
17 #include "llvm/Support/RISCVAttributes.h"
18 #include "llvm/Support/RISCVISAInfo.h"
19 
20 using namespace llvm;
21 
22 RISCVTargetStreamer::RISCVTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
23 
24 void RISCVTargetStreamer::finish() { finishAttributeSection(); }
25 
26 void RISCVTargetStreamer::emitDirectiveOptionPush() {}
27 void RISCVTargetStreamer::emitDirectiveOptionPop() {}
28 void RISCVTargetStreamer::emitDirectiveOptionPIC() {}
29 void RISCVTargetStreamer::emitDirectiveOptionNoPIC() {}
30 void RISCVTargetStreamer::emitDirectiveOptionRVC() {}
31 void RISCVTargetStreamer::emitDirectiveOptionNoRVC() {}
32 void RISCVTargetStreamer::emitDirectiveOptionRelax() {}
33 void RISCVTargetStreamer::emitDirectiveOptionNoRelax() {}
34 void RISCVTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}
35 void RISCVTargetStreamer::finishAttributeSection() {}
36 void RISCVTargetStreamer::emitTextAttribute(unsigned Attribute,
37                                             StringRef String) {}
38 void RISCVTargetStreamer::emitIntTextAttribute(unsigned Attribute,
39                                                unsigned IntValue,
40                                                StringRef StringValue) {}
41 
42 void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {
43   if (STI.hasFeature(RISCV::FeatureRV32E))
44     emitAttribute(RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_4);
45   else
46     emitAttribute(RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_16);
47 
48   auto ParseResult = RISCVFeatures::parseFeatureBits(
49       STI.hasFeature(RISCV::Feature64Bit), STI.getFeatureBits());
50   if (!ParseResult) {
51     /* Assume any error about features should handled earlier.  */
52     consumeError(ParseResult.takeError());
53     llvm_unreachable("Parsing feature error when emitTargetAttributes?");
54   } else {
55     auto &ISAInfo = *ParseResult;
56     emitTextAttribute(RISCVAttrs::ARCH, ISAInfo->toString());
57   }
58 }
59 
60 // This part is for ascii assembly output
61 RISCVTargetAsmStreamer::RISCVTargetAsmStreamer(MCStreamer &S,
62                                                formatted_raw_ostream &OS)
63     : RISCVTargetStreamer(S), OS(OS) {}
64 
65 void RISCVTargetAsmStreamer::emitDirectiveOptionPush() {
66   OS << "\t.option\tpush\n";
67 }
68 
69 void RISCVTargetAsmStreamer::emitDirectiveOptionPop() {
70   OS << "\t.option\tpop\n";
71 }
72 
73 void RISCVTargetAsmStreamer::emitDirectiveOptionPIC() {
74   OS << "\t.option\tpic\n";
75 }
76 
77 void RISCVTargetAsmStreamer::emitDirectiveOptionNoPIC() {
78   OS << "\t.option\tnopic\n";
79 }
80 
81 void RISCVTargetAsmStreamer::emitDirectiveOptionRVC() {
82   OS << "\t.option\trvc\n";
83 }
84 
85 void RISCVTargetAsmStreamer::emitDirectiveOptionNoRVC() {
86   OS << "\t.option\tnorvc\n";
87 }
88 
89 void RISCVTargetAsmStreamer::emitDirectiveOptionRelax() {
90   OS << "\t.option\trelax\n";
91 }
92 
93 void RISCVTargetAsmStreamer::emitDirectiveOptionNoRelax() {
94   OS << "\t.option\tnorelax\n";
95 }
96 
97 void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
98   OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n";
99 }
100 
101 void RISCVTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
102                                                StringRef String) {
103   OS << "\t.attribute\t" << Attribute << ", \"" << String << "\"\n";
104 }
105 
106 void RISCVTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
107                                                   unsigned IntValue,
108                                                   StringRef StringValue) {}
109 
110 void RISCVTargetAsmStreamer::finishAttributeSection() {}
111