LLVM 20.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
12#include "llvm/MC/MCContext.h"
15
16using namespace llvm;
17
18//
19// ConstantPool implementation
20//
21// Emit the contents of the constant pool using the provided streamer.
23 if (Entries.empty())
24 return;
25
26 if (CurrentSection != nullptr)
27 Streamer.switchSection(CurrentSection);
28
30 for (const ConstantPoolEntry &Entry : Entries) {
31 Streamer.emitCodeAlignment(
32 Align(Entry.Size),
33 Streamer.getContext().getSubtargetInfo()); // align naturally
34 Streamer.emitLabel(Entry.Label);
35 Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);
36 }
38 Entries.clear();
39}
40
42 const MCExpr *Value, unsigned Size,
43 SMLoc Loc, const MCExpr *AdjustExpr) {
44 if (CurrentSection == nullptr)
45 CurrentSection = Streamer.getCurrentSectionOnly();
46
47 auto &Context = Streamer.getContext();
48
49 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
50
51 // Check if there is existing entry for the same constant. If so, reuse it.
52 auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
53 if (Itr != CachedEntries.end())
54 return Itr->second;
55
56 MCSymbol *CPEntryLabel = Context.createTempSymbol();
57 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
58
59 if (AdjustExpr) {
60 const CSKYMCExpr *CSKYExpr = cast<CSKYMCExpr>(Value);
61
62 Value = MCBinaryExpr::createSub(AdjustExpr, SymRef, Context);
63 Value = MCBinaryExpr::createSub(CSKYExpr->getSubExpr(), Value, Context);
64 Value = CSKYMCExpr::create(Value, CSKYExpr->getKind(), Context);
65 }
66
67 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
68
69 if (C)
70 CachedEntries[C->getValue()] = SymRef;
71 return SymRef;
72}
73
74bool CSKYConstantPool::empty() { return Entries.empty(); }
75
77 CurrentSection = nullptr;
78 CachedEntries.clear();
79}
80
83
84const MCExpr *
86 const MCExpr *AdjustExpr) {
87 auto ELFRefKind = CSKYMCExpr::VK_CSKY_Invalid;
89
90 const MCExpr *OrigExpr = Expr;
91
92 if (const CSKYMCExpr *CE = dyn_cast<CSKYMCExpr>(Expr)) {
93 Expr = CE->getSubExpr();
94 ELFRefKind = CE->getKind();
95 }
96
97 if (const MCSymbolRefExpr *SymExpr = dyn_cast<MCSymbolRefExpr>(Expr)) {
98 const MCSymbol *Sym = &SymExpr->getSymbol();
99
100 SymbolIndex Index = {Sym, ELFRefKind};
101
102 if (ConstantMap.find(Index) == ConstantMap.end()) {
103 ConstantMap[Index] =
104 ConstantPool->addEntry(getStreamer(), OrigExpr, 4, Loc, AdjustExpr);
105 }
106 return ConstantMap[Index];
107 }
108
109 return ConstantPool->addEntry(getStreamer(), Expr, 4, Loc, AdjustExpr);
110}
111
113 ConstantPool->emitAll(Streamer);
115}
116
117// finish() - write out any non-empty assembler constant pools.
119 if (ConstantCounter != 0) {
120 ConstantPool->emitAll(Streamer);
121 }
122
124}
125
127
130 StringRef String) {}
132
133void CSKYTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
134 OS << "\t.csky_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
135}
136
137void CSKYTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
139 OS << "\t.csky_attribute\t" << Attribute << ", \"" << String << "\"\n";
140}
141
142void CSKYTargetAsmStreamer::finishAttributeSection() {}
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
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:622
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCContext.h:418
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCContext & getContext() const
Definition: MCStreamer.h:300
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:179
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:420
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:470
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:400
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:398
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:94
MCStreamer & getStreamer()
Definition: MCStreamer.h:102
MCStreamer & Streamer
Definition: MCStreamer.h:96
Represents a location in source code.
Definition: SMLoc.h:23
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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:66
@ MCDR_DataRegion
.data_region
Definition: MCDirectives.h:62
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39