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 unsigned XLen = STI.hasFeature(RISCV::Feature64Bit) ? 64 : 32; 49 std::vector<std::string> FeatureVector; 50 RISCVFeatures::toFeatureVector(FeatureVector, STI.getFeatureBits()); 51 52 auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector); 53 if (!ParseResult) { 54 /* Assume any error about features should handled earlier. */ 55 consumeError(ParseResult.takeError()); 56 llvm_unreachable("Parsing feature error when emitTargetAttributes?"); 57 } else { 58 auto &ISAInfo = *ParseResult; 59 emitTextAttribute(RISCVAttrs::ARCH, ISAInfo->toString()); 60 } 61 } 62 63 // This part is for ascii assembly output 64 RISCVTargetAsmStreamer::RISCVTargetAsmStreamer(MCStreamer &S, 65 formatted_raw_ostream &OS) 66 : RISCVTargetStreamer(S), OS(OS) {} 67 68 void RISCVTargetAsmStreamer::emitDirectiveOptionPush() { 69 OS << "\t.option\tpush\n"; 70 } 71 72 void RISCVTargetAsmStreamer::emitDirectiveOptionPop() { 73 OS << "\t.option\tpop\n"; 74 } 75 76 void RISCVTargetAsmStreamer::emitDirectiveOptionPIC() { 77 OS << "\t.option\tpic\n"; 78 } 79 80 void RISCVTargetAsmStreamer::emitDirectiveOptionNoPIC() { 81 OS << "\t.option\tnopic\n"; 82 } 83 84 void RISCVTargetAsmStreamer::emitDirectiveOptionRVC() { 85 OS << "\t.option\trvc\n"; 86 } 87 88 void RISCVTargetAsmStreamer::emitDirectiveOptionNoRVC() { 89 OS << "\t.option\tnorvc\n"; 90 } 91 92 void RISCVTargetAsmStreamer::emitDirectiveOptionRelax() { 93 OS << "\t.option\trelax\n"; 94 } 95 96 void RISCVTargetAsmStreamer::emitDirectiveOptionNoRelax() { 97 OS << "\t.option\tnorelax\n"; 98 } 99 100 void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) { 101 OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n"; 102 } 103 104 void RISCVTargetAsmStreamer::emitTextAttribute(unsigned Attribute, 105 StringRef String) { 106 OS << "\t.attribute\t" << Attribute << ", \"" << String << "\"\n"; 107 } 108 109 void RISCVTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute, 110 unsigned IntValue, 111 StringRef StringValue) {} 112 113 void RISCVTargetAsmStreamer::finishAttributeSection() {} 114