| 1 | //===-- RISCVELFStreamer.h - RISC-V ELF Target Streamer ---------*- C++ -*-===// |
| 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 | #ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVELFSTREAMER_H |
| 10 | #define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVELFSTREAMER_H |
| 11 | |
| 12 | #include "RISCVTargetStreamer.h" |
| 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/MC/MCELFStreamer.h" |
| 15 | |
| 16 | namespace llvm { |
| 17 | |
| 18 | class RISCVELFStreamer : public MCELFStreamer { |
| 19 | void reset() override; |
| 20 | void emitDataMappingSymbol(); |
| 21 | void emitInstructionsMappingSymbol(); |
| 22 | void emitMappingSymbol(StringRef Name); |
| 23 | |
| 24 | enum ElfMappingSymbol { EMS_None, EMS_Instructions, EMS_Data }; |
| 25 | |
| 26 | DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols; |
| 27 | ElfMappingSymbol LastEMS = EMS_None; |
| 28 | |
| 29 | // Active ISA string propagated from RISCVTargetELFStreamer. When non-empty, |
| 30 | // it is used as the suffix for "$x<ISA>" mapping symbols. |
| 31 | std::string MappingSymbolArch; |
| 32 | |
| 33 | // ISA suffix last emitted via "$x<ISA>" in the current section, and the |
| 34 | // per-section history preserved across changeSection. A new mapping symbol |
| 35 | // is emitted whenever LastEMS != EMS_Instructions or |
| 36 | // LastEmittedArch != MappingSymbolArch, so the ISA in effect at each |
| 37 | // instruction run is always recorded. |
| 38 | std::string LastEmittedArch; |
| 39 | DenseMap<const MCSection *, std::string> LastEmittedArchInSection; |
| 40 | |
| 41 | public: |
| 42 | RISCVELFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> MAB, |
| 43 | std::unique_ptr<MCObjectWriter> MOW, |
| 44 | std::unique_ptr<MCCodeEmitter> MCE); |
| 45 | |
| 46 | void changeSection(MCSection *Section, uint32_t Subsection) override; |
| 47 | void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override; |
| 48 | void emitBytes(StringRef Data) override; |
| 49 | void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc) override; |
| 50 | void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override; |
| 51 | |
| 52 | void setMappingSymbolArch(StringRef Arch); |
| 53 | }; |
| 54 | |
| 55 | class RISCVTargetELFStreamer : public RISCVTargetStreamer { |
| 56 | private: |
| 57 | StringRef CurrentVendor; |
| 58 | |
| 59 | // Initial ISA string derived from the subtarget features in the constructor. |
| 60 | // Used to re-establish state on reset(). |
| 61 | std::string InitialArchString; |
| 62 | |
| 63 | // Current ISA string, kept in sync with each .option arch/rvc/norvc/pop |
| 64 | // and .attribute arch directive. Used to avoid propagating redundant ISA |
| 65 | // updates to the streamer when the ISA does not actually change (e.g., |
| 66 | // .option rvc when C is already enabled). |
| 67 | std::string ArchString; |
| 68 | SmallVector<std::string, 4> ArchStringStack; |
| 69 | |
| 70 | MCSection *AttributeSection = nullptr; |
| 71 | |
| 72 | void emitAttribute(unsigned Attribute, unsigned Value) override; |
| 73 | void emitTextAttribute(unsigned Attribute, StringRef String) override; |
| 74 | void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, |
| 75 | StringRef StringValue) override; |
| 76 | void finishAttributeSection() override; |
| 77 | |
| 78 | void reset() override; |
| 79 | |
| 80 | public: |
| 81 | RISCVELFStreamer &getStreamer(); |
| 82 | RISCVTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI); |
| 83 | |
| 84 | // Update ArchString and propagate the change to the streamer so the next |
| 85 | // instruction-run emits an ISA-specific mapping symbol. A no-op when |
| 86 | // Arch == ArchString (deduplication). |
| 87 | void setArchString(StringRef Arch) override; |
| 88 | void emitDirectiveOptionExact() override; |
| 89 | void emitDirectiveOptionNoExact() override; |
| 90 | void emitDirectiveOptionPIC() override; |
| 91 | void emitDirectiveOptionNoPIC() override; |
| 92 | void emitDirectiveOptionPop() override; |
| 93 | void emitDirectiveOptionPush() override; |
| 94 | void emitDirectiveOptionRelax() override; |
| 95 | void emitDirectiveOptionNoRelax() override; |
| 96 | void emitDirectiveOptionRVC() override; |
| 97 | void emitDirectiveOptionNoRVC() override; |
| 98 | void emitDirectiveVariantCC(MCSymbol &Symbol) override; |
| 99 | |
| 100 | void emitNoteGnuPropertySection(const uint32_t Feature1And); |
| 101 | void finish() override; |
| 102 | }; |
| 103 | |
| 104 | MCStreamer *createRISCVELFStreamer(const Triple &, MCContext &C, |
| 105 | std::unique_ptr<MCAsmBackend> &&MAB, |
| 106 | std::unique_ptr<MCObjectWriter> &&MOW, |
| 107 | std::unique_ptr<MCCodeEmitter> &&MCE); |
| 108 | } // namespace llvm |
| 109 | |
| 110 | #endif // LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVELFSTREAMER_H |
| 111 | |