LLVM 22.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
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 auto *CSKYExpr = cast<MCSpecifierExpr>(Value);
62
63 Value = MCBinaryExpr::createSub(AdjustExpr, SymRef, Context);
64 Value = MCBinaryExpr::createSub(CSKYExpr->getSubExpr(), Value, Context);
65 Value = MCSpecifierExpr::create(Value, CSKYExpr->getSpecifier(), 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 uint8_t ELFRefKind = CSKY::S_Invalid;
90
91 const MCExpr *OrigExpr = Expr;
92
93 if (auto *CE = dyn_cast<MCSpecifierExpr>(Expr)) {
94 Expr = CE->getSubExpr();
95 ELFRefKind = CE->getSpecifier();
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()) {
104 ConstantMap[Index] =
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:479
void emitAll(MCStreamer &Streamer)
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Value, unsigned Size, SMLoc Loc, const MCExpr *AdjustExpr)
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:428
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
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:743
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCContext & getContext() const
Definition: MCStreamer.h:314
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:178
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:395
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:495
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:421
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:190
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
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:82
void push_back(const T &Elt)
Definition: SmallVector.h:414
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
LLVM Value Representation.
Definition: Value.h:75
@ 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:58
@ MCDR_DataRegion
.data_region
Definition: MCDirectives.h:54
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39