Path: blob/main/contrib/llvm-project/llvm/lib/Target/CSKY/MCTargetDesc/CSKYTargetStreamer.cpp
35294 views
//===-- CSKYTargetStreamer.h - CSKY 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#include "CSKYTargetStreamer.h"9#include "llvm/CodeGen/MachineFrameInfo.h"10#include "llvm/CodeGen/TargetSubtargetInfo.h"11#include "llvm/MC/MCContext.h"12#include "llvm/MC/MCSectionELF.h"13#include "llvm/Support/FormattedStream.h"1415using namespace llvm;1617//18// ConstantPool implementation19//20// Emit the contents of the constant pool using the provided streamer.21void CSKYConstantPool::emitAll(MCStreamer &Streamer) {22if (Entries.empty())23return;2425if (CurrentSection != nullptr)26Streamer.switchSection(CurrentSection);2728Streamer.emitDataRegion(MCDR_DataRegion);29for (const ConstantPoolEntry &Entry : Entries) {30Streamer.emitCodeAlignment(31Align(Entry.Size),32Streamer.getContext().getSubtargetInfo()); // align naturally33Streamer.emitLabel(Entry.Label);34Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);35}36Streamer.emitDataRegion(MCDR_DataRegionEnd);37Entries.clear();38}3940const MCExpr *CSKYConstantPool::addEntry(MCStreamer &Streamer,41const MCExpr *Value, unsigned Size,42SMLoc Loc, const MCExpr *AdjustExpr) {43if (CurrentSection == nullptr)44CurrentSection = Streamer.getCurrentSectionOnly();4546auto &Context = Streamer.getContext();4748const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);4950// Check if there is existing entry for the same constant. If so, reuse it.51auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();52if (Itr != CachedEntries.end())53return Itr->second;5455MCSymbol *CPEntryLabel = Context.createTempSymbol();56const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);5758if (AdjustExpr) {59const CSKYMCExpr *CSKYExpr = cast<CSKYMCExpr>(Value);6061Value = MCBinaryExpr::createSub(AdjustExpr, SymRef, Context);62Value = MCBinaryExpr::createSub(CSKYExpr->getSubExpr(), Value, Context);63Value = CSKYMCExpr::create(Value, CSKYExpr->getKind(), Context);64}6566Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));6768if (C)69CachedEntries[C->getValue()] = SymRef;70return SymRef;71}7273bool CSKYConstantPool::empty() { return Entries.empty(); }7475void CSKYConstantPool::clearCache() {76CurrentSection = nullptr;77CachedEntries.clear();78}7980CSKYTargetStreamer::CSKYTargetStreamer(MCStreamer &S)81: MCTargetStreamer(S), ConstantPool(new CSKYConstantPool()) {}8283const MCExpr *84CSKYTargetStreamer::addConstantPoolEntry(const MCExpr *Expr, SMLoc Loc,85const MCExpr *AdjustExpr) {86auto ELFRefKind = CSKYMCExpr::VK_CSKY_Invalid;87ConstantCounter++;8889const MCExpr *OrigExpr = Expr;9091if (const CSKYMCExpr *CE = dyn_cast<CSKYMCExpr>(Expr)) {92Expr = CE->getSubExpr();93ELFRefKind = CE->getKind();94}9596if (const MCSymbolRefExpr *SymExpr = dyn_cast<MCSymbolRefExpr>(Expr)) {97const MCSymbol *Sym = &SymExpr->getSymbol();9899SymbolIndex Index = {Sym, ELFRefKind};100101if (ConstantMap.find(Index) == ConstantMap.end()) {102ConstantMap[Index] =103ConstantPool->addEntry(getStreamer(), OrigExpr, 4, Loc, AdjustExpr);104}105return ConstantMap[Index];106}107108return ConstantPool->addEntry(getStreamer(), Expr, 4, Loc, AdjustExpr);109}110111void CSKYTargetStreamer::emitCurrentConstantPool() {112ConstantPool->emitAll(Streamer);113ConstantPool->clearCache();114}115116// finish() - write out any non-empty assembler constant pools.117void CSKYTargetStreamer::finish() {118if (ConstantCounter != 0) {119ConstantPool->emitAll(Streamer);120}121122finishAttributeSection();123}124125void CSKYTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {}126127void CSKYTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}128void CSKYTargetStreamer::emitTextAttribute(unsigned Attribute,129StringRef String) {}130void CSKYTargetStreamer::finishAttributeSection() {}131132void CSKYTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {133OS << "\t.csky_attribute\t" << Attribute << ", " << Twine(Value) << "\n";134}135136void CSKYTargetAsmStreamer::emitTextAttribute(unsigned Attribute,137StringRef String) {138OS << "\t.csky_attribute\t" << Attribute << ", \"" << String << "\"\n";139}140141void CSKYTargetAsmStreamer::finishAttributeSection() {}142143144