Path: blob/main/contrib/llvm-project/llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFStreamer.h
35294 views
//===-- CSKYELFStreamer.h - CSKY ELF Target Streamer -----------*- C++ -*--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_TARGET_CSKY_CSKYELFSTREAMER_H9#define LLVM_LIB_TARGET_CSKY_CSKYELFSTREAMER_H1011#include "CSKYTargetStreamer.h"12#include "llvm/MC/MCCodeEmitter.h"13#include "llvm/MC/MCELFStreamer.h"14#include "llvm/MC/MCObjectWriter.h"1516namespace llvm {1718class CSKYTargetELFStreamer : public CSKYTargetStreamer {19private:20enum class AttributeType { Hidden, Numeric, Text, NumericAndText };2122struct AttributeItem {23AttributeType Type;24unsigned Tag;25unsigned IntValue;26std::string StringValue;27};2829StringRef CurrentVendor;30SmallVector<AttributeItem, 64> Contents;3132MCSection *AttributeSection = nullptr;3334AttributeItem *getAttributeItem(unsigned Attribute) {35for (size_t i = 0; i < Contents.size(); ++i)36if (Contents[i].Tag == Attribute)37return &Contents[i];38return nullptr;39}4041void setAttributeItem(unsigned Attribute, unsigned Value,42bool OverwriteExisting) {43// Look for existing attribute item.44if (AttributeItem *Item = getAttributeItem(Attribute)) {45if (!OverwriteExisting)46return;47Item->Type = AttributeType::Numeric;48Item->IntValue = Value;49return;50}5152// Create new attribute item.53Contents.push_back({AttributeType::Numeric, Attribute, Value, ""});54}5556void setAttributeItem(unsigned Attribute, StringRef Value,57bool OverwriteExisting) {58// Look for existing attribute item.59if (AttributeItem *Item = getAttributeItem(Attribute)) {60if (!OverwriteExisting)61return;62Item->Type = AttributeType::Text;63Item->StringValue = std::string(Value);64return;65}6667// Create new attribute item.68Contents.push_back({AttributeType::Text, Attribute, 0, std::string(Value)});69}7071void setAttributeItems(unsigned Attribute, unsigned IntValue,72StringRef StringValue, bool OverwriteExisting) {73// Look for existing attribute item.74if (AttributeItem *Item = getAttributeItem(Attribute)) {75if (!OverwriteExisting)76return;77Item->Type = AttributeType::NumericAndText;78Item->IntValue = IntValue;79Item->StringValue = std::string(StringValue);80return;81}8283// Create new attribute item.84Contents.push_back({AttributeType::NumericAndText, Attribute, IntValue,85std::string(StringValue)});86}8788void emitAttribute(unsigned Attribute, unsigned Value) override;89void emitTextAttribute(unsigned Attribute, StringRef String) override;90void finishAttributeSection() override;91size_t calculateContentSize() const;9293void emitTargetAttributes(const MCSubtargetInfo &STI) override;9495public:96MCELFStreamer &getStreamer();97CSKYTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);98};99100class CSKYELFStreamer : public MCELFStreamer {101void EmitMappingSymbol(StringRef Name);102103public:104friend class CSKYTargetELFStreamer;105106enum ElfMappingSymbol { EMS_None, EMS_Text, EMS_Data };107108ElfMappingSymbol State;109110CSKYELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,111std::unique_ptr<MCObjectWriter> OW,112std::unique_ptr<MCCodeEmitter> Emitter)113: MCELFStreamer(Context, std::move(TAB), std::move(OW),114std::move(Emitter)),115State(EMS_None) {}116117~CSKYELFStreamer() override = default;118119void emitFill(const MCExpr &NumBytes, uint64_t FillValue,120SMLoc Loc) override {121EmitMappingSymbol("$d");122MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);123}124void emitBytes(StringRef Data) override {125EmitMappingSymbol("$d");126MCELFStreamer::emitBytes(Data);127}128void emitInstruction(const MCInst &Inst,129const MCSubtargetInfo &STI) override {130EmitMappingSymbol("$t");131MCELFStreamer::emitInstruction(Inst, STI);132}133void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {134EmitMappingSymbol("$d");135MCELFStreamer::emitValueImpl(Value, Size, Loc);136}137void reset() override {138State = EMS_None;139MCELFStreamer::reset();140}141};142143} // namespace llvm144#endif145146147