xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/CSKY/MCTargetDesc/CSKYTargetStreamer.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
181ad6265SDimitry Andric //===-- CSKYTargetStreamer.h - CSKY Target Streamer ----------*- C++ -*----===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #include "CSKYTargetStreamer.h"
1081ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
1181ad6265SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
1281ad6265SDimitry Andric #include "llvm/MC/MCContext.h"
1381ad6265SDimitry Andric #include "llvm/MC/MCSectionELF.h"
1481ad6265SDimitry Andric #include "llvm/Support/FormattedStream.h"
1581ad6265SDimitry Andric 
1681ad6265SDimitry Andric using namespace llvm;
1781ad6265SDimitry Andric 
1881ad6265SDimitry Andric //
1981ad6265SDimitry Andric // ConstantPool implementation
2081ad6265SDimitry Andric //
2181ad6265SDimitry Andric // Emit the contents of the constant pool using the provided streamer.
emitAll(MCStreamer & Streamer)2281ad6265SDimitry Andric void CSKYConstantPool::emitAll(MCStreamer &Streamer) {
2381ad6265SDimitry Andric   if (Entries.empty())
2481ad6265SDimitry Andric     return;
2581ad6265SDimitry Andric 
2681ad6265SDimitry Andric   if (CurrentSection != nullptr)
2781ad6265SDimitry Andric     Streamer.switchSection(CurrentSection);
2881ad6265SDimitry Andric 
2981ad6265SDimitry Andric   Streamer.emitDataRegion(MCDR_DataRegion);
3081ad6265SDimitry Andric   for (const ConstantPoolEntry &Entry : Entries) {
3181ad6265SDimitry Andric     Streamer.emitCodeAlignment(
32*bdd1243dSDimitry Andric         Align(Entry.Size),
3381ad6265SDimitry Andric         Streamer.getContext().getSubtargetInfo()); // align naturally
3481ad6265SDimitry Andric     Streamer.emitLabel(Entry.Label);
3581ad6265SDimitry Andric     Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);
3681ad6265SDimitry Andric   }
3781ad6265SDimitry Andric   Streamer.emitDataRegion(MCDR_DataRegionEnd);
3881ad6265SDimitry Andric   Entries.clear();
3981ad6265SDimitry Andric }
4081ad6265SDimitry Andric 
addEntry(MCStreamer & Streamer,const MCExpr * Value,unsigned Size,SMLoc Loc,const MCExpr * AdjustExpr)4181ad6265SDimitry Andric const MCExpr *CSKYConstantPool::addEntry(MCStreamer &Streamer,
4281ad6265SDimitry Andric                                          const MCExpr *Value, unsigned Size,
4381ad6265SDimitry Andric                                          SMLoc Loc, const MCExpr *AdjustExpr) {
4481ad6265SDimitry Andric   if (CurrentSection == nullptr)
4581ad6265SDimitry Andric     CurrentSection = Streamer.getCurrentSectionOnly();
4681ad6265SDimitry Andric 
4781ad6265SDimitry Andric   auto &Context = Streamer.getContext();
4881ad6265SDimitry Andric 
4981ad6265SDimitry Andric   const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
5081ad6265SDimitry Andric 
5181ad6265SDimitry Andric   // Check if there is existing entry for the same constant. If so, reuse it.
5281ad6265SDimitry Andric   auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
5381ad6265SDimitry Andric   if (Itr != CachedEntries.end())
5481ad6265SDimitry Andric     return Itr->second;
5581ad6265SDimitry Andric 
5681ad6265SDimitry Andric   MCSymbol *CPEntryLabel = Context.createTempSymbol();
5781ad6265SDimitry Andric   const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
5881ad6265SDimitry Andric 
5981ad6265SDimitry Andric   if (AdjustExpr) {
6081ad6265SDimitry Andric     const CSKYMCExpr *CSKYExpr = cast<CSKYMCExpr>(Value);
6181ad6265SDimitry Andric 
6281ad6265SDimitry Andric     Value = MCBinaryExpr::createSub(AdjustExpr, SymRef, Context);
6381ad6265SDimitry Andric     Value = MCBinaryExpr::createSub(CSKYExpr->getSubExpr(), Value, Context);
6481ad6265SDimitry Andric     Value = CSKYMCExpr::create(Value, CSKYExpr->getKind(), Context);
6581ad6265SDimitry Andric   }
6681ad6265SDimitry Andric 
6781ad6265SDimitry Andric   Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
6881ad6265SDimitry Andric 
6981ad6265SDimitry Andric   if (C)
7081ad6265SDimitry Andric     CachedEntries[C->getValue()] = SymRef;
7181ad6265SDimitry Andric   return SymRef;
7281ad6265SDimitry Andric }
7381ad6265SDimitry Andric 
empty()7481ad6265SDimitry Andric bool CSKYConstantPool::empty() { return Entries.empty(); }
7581ad6265SDimitry Andric 
clearCache()7681ad6265SDimitry Andric void CSKYConstantPool::clearCache() {
7781ad6265SDimitry Andric   CurrentSection = nullptr;
7881ad6265SDimitry Andric   CachedEntries.clear();
7981ad6265SDimitry Andric }
8081ad6265SDimitry Andric 
CSKYTargetStreamer(MCStreamer & S)8181ad6265SDimitry Andric CSKYTargetStreamer::CSKYTargetStreamer(MCStreamer &S)
8281ad6265SDimitry Andric     : MCTargetStreamer(S), ConstantPool(new CSKYConstantPool()) {}
8381ad6265SDimitry Andric 
8481ad6265SDimitry Andric const MCExpr *
addConstantPoolEntry(const MCExpr * Expr,SMLoc Loc,const MCExpr * AdjustExpr)8581ad6265SDimitry Andric CSKYTargetStreamer::addConstantPoolEntry(const MCExpr *Expr, SMLoc Loc,
8681ad6265SDimitry Andric                                          const MCExpr *AdjustExpr) {
8781ad6265SDimitry Andric   auto ELFRefKind = CSKYMCExpr::VK_CSKY_Invalid;
8881ad6265SDimitry Andric   ConstantCounter++;
8981ad6265SDimitry Andric 
9081ad6265SDimitry Andric   const MCExpr *OrigExpr = Expr;
9181ad6265SDimitry Andric 
9281ad6265SDimitry Andric   if (const CSKYMCExpr *CE = dyn_cast<CSKYMCExpr>(Expr)) {
9381ad6265SDimitry Andric     Expr = CE->getSubExpr();
9481ad6265SDimitry Andric     ELFRefKind = CE->getKind();
9581ad6265SDimitry Andric   }
9681ad6265SDimitry Andric 
9781ad6265SDimitry Andric   if (const MCSymbolRefExpr *SymExpr = dyn_cast<MCSymbolRefExpr>(Expr)) {
9881ad6265SDimitry Andric     const MCSymbol *Sym = &SymExpr->getSymbol();
9981ad6265SDimitry Andric 
10081ad6265SDimitry Andric     SymbolIndex Index = {Sym, ELFRefKind};
10181ad6265SDimitry Andric 
10281ad6265SDimitry Andric     if (ConstantMap.find(Index) == ConstantMap.end()) {
10381ad6265SDimitry Andric       ConstantMap[Index] =
10481ad6265SDimitry Andric           ConstantPool->addEntry(getStreamer(), OrigExpr, 4, Loc, AdjustExpr);
10581ad6265SDimitry Andric     }
10681ad6265SDimitry Andric     return ConstantMap[Index];
10781ad6265SDimitry Andric   }
10881ad6265SDimitry Andric 
10981ad6265SDimitry Andric   return ConstantPool->addEntry(getStreamer(), Expr, 4, Loc, AdjustExpr);
11081ad6265SDimitry Andric }
11181ad6265SDimitry Andric 
emitCurrentConstantPool()11281ad6265SDimitry Andric void CSKYTargetStreamer::emitCurrentConstantPool() {
11381ad6265SDimitry Andric   ConstantPool->emitAll(Streamer);
11481ad6265SDimitry Andric   ConstantPool->clearCache();
11581ad6265SDimitry Andric }
11681ad6265SDimitry Andric 
11781ad6265SDimitry Andric // finish() - write out any non-empty assembler constant pools.
finish()11881ad6265SDimitry Andric void CSKYTargetStreamer::finish() {
11981ad6265SDimitry Andric   if (ConstantCounter != 0) {
12081ad6265SDimitry Andric     ConstantPool->emitAll(Streamer);
12181ad6265SDimitry Andric   }
12281ad6265SDimitry Andric 
12381ad6265SDimitry Andric   finishAttributeSection();
12481ad6265SDimitry Andric }
12581ad6265SDimitry Andric 
emitTargetAttributes(const MCSubtargetInfo & STI)12681ad6265SDimitry Andric void CSKYTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {}
12781ad6265SDimitry Andric 
emitAttribute(unsigned Attribute,unsigned Value)12881ad6265SDimitry Andric void CSKYTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}
emitTextAttribute(unsigned Attribute,StringRef String)12981ad6265SDimitry Andric void CSKYTargetStreamer::emitTextAttribute(unsigned Attribute,
13081ad6265SDimitry Andric                                            StringRef String) {}
finishAttributeSection()13181ad6265SDimitry Andric void CSKYTargetStreamer::finishAttributeSection() {}
13281ad6265SDimitry Andric 
emitAttribute(unsigned Attribute,unsigned Value)13381ad6265SDimitry Andric void CSKYTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
13481ad6265SDimitry Andric   OS << "\t.csky_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
13581ad6265SDimitry Andric }
13681ad6265SDimitry Andric 
emitTextAttribute(unsigned Attribute,StringRef String)13781ad6265SDimitry Andric void CSKYTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
13881ad6265SDimitry Andric                                               StringRef String) {
13981ad6265SDimitry Andric   OS << "\t.csky_attribute\t" << Attribute << ", \"" << String << "\"\n";
14081ad6265SDimitry Andric }
14181ad6265SDimitry Andric 
finishAttributeSection()14281ad6265SDimitry Andric void CSKYTargetAsmStreamer::finishAttributeSection() {}
143