LLVM 17.0.0git
CSKYTargetStreamer.cpp
Go to the documentation of this file.
1//===-- CSKYTargetStreamer.h - CSKY 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
10#include "CSKYSubtarget.h"
13#include "llvm/MC/MCContext.h"
16
17using namespace llvm;
18
19//
20// ConstantPool implementation
21//
22// Emit the contents of the constant pool using the provided streamer.
24 if (Entries.empty())
25 return;
26
27 if (CurrentSection != nullptr)
28 Streamer.switchSection(CurrentSection);
29
31 for (const ConstantPoolEntry &Entry : Entries) {
32 Streamer.emitCodeAlignment(
33 Align(Entry.Size),
34 Streamer.getContext().getSubtargetInfo()); // align naturally
35 Streamer.emitLabel(Entry.Label);
36 Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);
37 }
39 Entries.clear();
40}
41
43 const MCExpr *Value, unsigned Size,
44 SMLoc Loc, const MCExpr *AdjustExpr) {
45 if (CurrentSection == nullptr)
46 CurrentSection = Streamer.getCurrentSectionOnly();
47
48 auto &Context = Streamer.getContext();
49
50 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
51
52 // Check if there is existing entry for the same constant. If so, reuse it.
53 auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
54 if (Itr != CachedEntries.end())
55 return Itr->second;
56
57 MCSymbol *CPEntryLabel = Context.createTempSymbol();
58 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
59
60 if (AdjustExpr) {
61 const CSKYMCExpr *CSKYExpr = cast<CSKYMCExpr>(Value);
62
63 Value = MCBinaryExpr::createSub(AdjustExpr, SymRef, Context);
66 }
67
68 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
69
70 if (C)
71 CachedEntries[C->getValue()] = SymRef;
72 return SymRef;
73}
74
75bool CSKYConstantPool::empty() { return Entries.empty(); }
76
78 CurrentSection = nullptr;
79 CachedEntries.clear();
80}
81
84
85const MCExpr *
87 const MCExpr *AdjustExpr) {
88 auto ELFRefKind = CSKYMCExpr::VK_CSKY_Invalid;
90
91 const MCExpr *OrigExpr = Expr;
92
93 if (const CSKYMCExpr *CE = dyn_cast<CSKYMCExpr>(Expr)) {
94 Expr = CE->getSubExpr();
95 ELFRefKind = CE->getKind();
96 }
97
98 if (const MCSymbolRefExpr *SymExpr = dyn_cast<MCSymbolRefExpr>(Expr)) {
99 const MCSymbol *Sym = &SymExpr->getSymbol();
100
101 SymbolIndex Index = {Sym, ELFRefKind};
102
103 if (ConstantMap.find(Index) == ConstantMap.end()) {
105 ConstantPool->addEntry(getStreamer(), OrigExpr, 4, Loc, AdjustExpr);
106 }
107 return ConstantMap[Index];
108 }
109
110 return ConstantPool->addEntry(getStreamer(), Expr, 4, Loc, AdjustExpr);
111}
112
114 ConstantPool->emitAll(Streamer);
116}
117
118// finish() - write out any non-empty assembler constant pools.
120 if (ConstantCounter != 0) {
121 ConstantPool->emitAll(Streamer);
122 }
123
125}
126
128
131 StringRef String) {}
133
134void CSKYTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
135 OS << "\t.csky_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
136}
137
138void CSKYTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
140 OS << "\t.csky_attribute\t" << Attribute << ", \"" << String << "\"\n";
141}
142
143void CSKYTargetAsmStreamer::finishAttributeSection() {}
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:463
LLVMContext & Context
void emitAll(MCStreamer &Streamer)
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Value, unsigned Size, SMLoc Loc, const MCExpr *AdjustExpr)
const MCExpr * getSubExpr() const
Definition: CSKYMCExpr.h:54
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:22
VariantKind getKind() const
Definition: CSKYMCExpr.h:51
virtual void emitTextAttribute(unsigned Attribute, StringRef String)
virtual void emitTargetAttributes(const MCSubtargetInfo &STI)
DenseMap< SymbolIndex, const MCExpr * > ConstantMap
const MCExpr * addConstantPoolEntry(const MCExpr *, SMLoc Loc, const MCExpr *AdjustExpr=nullptr)
Add a new entry to the constant pool for the current section and return an MCExpr that can be used to...
virtual void emitAttribute(unsigned Attribute, unsigned Value)
const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc)
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:610
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCContext.h:452
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCContext & getContext() const
Definition: MCStreamer.h:297
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:180
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:424
virtual void emitCodeAlignment(Align Alignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0)
Emit nops until the byte alignment ByteAlignment is reached.
virtual void emitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:503
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:393
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Target specific streamer interface.
Definition: MCStreamer.h:93
MCStreamer & getStreamer()
Definition: MCStreamer.h:101
MCStreamer & Streamer
Definition: MCStreamer.h:95
Represents a location in source code.
Definition: SMLoc.h:23
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:416
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ MCDR_DataRegionEnd
.end_data_region
Definition: MCDirectives.h:65
@ MCDR_DataRegion
.data_region
Definition: MCDirectives.h:61
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39