xref: /aosp_15_r20/external/llvm/lib/MC/ConstantPools.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- ConstantPools.cpp - ConstantPool class --*- C++ -*---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the ConstantPool and  AssemblerConstantPools classes.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/MapVector.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/ConstantPools.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCExpr.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCStreamer.h"
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker using namespace llvm;
20*9880d681SAndroid Build Coastguard Worker //
21*9880d681SAndroid Build Coastguard Worker // ConstantPool implementation
22*9880d681SAndroid Build Coastguard Worker //
23*9880d681SAndroid Build Coastguard Worker // Emit the contents of the constant pool using the provided streamer.
emitEntries(MCStreamer & Streamer)24*9880d681SAndroid Build Coastguard Worker void ConstantPool::emitEntries(MCStreamer &Streamer) {
25*9880d681SAndroid Build Coastguard Worker   if (Entries.empty())
26*9880d681SAndroid Build Coastguard Worker     return;
27*9880d681SAndroid Build Coastguard Worker   Streamer.EmitDataRegion(MCDR_DataRegion);
28*9880d681SAndroid Build Coastguard Worker   for (const ConstantPoolEntry &Entry : Entries) {
29*9880d681SAndroid Build Coastguard Worker     Streamer.EmitCodeAlignment(Entry.Size); // align naturally
30*9880d681SAndroid Build Coastguard Worker     Streamer.EmitLabel(Entry.Label);
31*9880d681SAndroid Build Coastguard Worker     Streamer.EmitValue(Entry.Value, Entry.Size, Entry.Loc);
32*9880d681SAndroid Build Coastguard Worker   }
33*9880d681SAndroid Build Coastguard Worker   Streamer.EmitDataRegion(MCDR_DataRegionEnd);
34*9880d681SAndroid Build Coastguard Worker   Entries.clear();
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker 
addEntry(const MCExpr * Value,MCContext & Context,unsigned Size,SMLoc Loc)37*9880d681SAndroid Build Coastguard Worker const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
38*9880d681SAndroid Build Coastguard Worker                                      unsigned Size, SMLoc Loc) {
39*9880d681SAndroid Build Coastguard Worker   MCSymbol *CPEntryLabel = Context.createTempSymbol();
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker   Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
42*9880d681SAndroid Build Coastguard Worker   return MCSymbolRefExpr::create(CPEntryLabel, Context);
43*9880d681SAndroid Build Coastguard Worker }
44*9880d681SAndroid Build Coastguard Worker 
empty()45*9880d681SAndroid Build Coastguard Worker bool ConstantPool::empty() { return Entries.empty(); }
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker //
48*9880d681SAndroid Build Coastguard Worker // AssemblerConstantPools implementation
49*9880d681SAndroid Build Coastguard Worker //
getConstantPool(MCSection * Section)50*9880d681SAndroid Build Coastguard Worker ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
51*9880d681SAndroid Build Coastguard Worker   ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
52*9880d681SAndroid Build Coastguard Worker   if (CP == ConstantPools.end())
53*9880d681SAndroid Build Coastguard Worker     return nullptr;
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker   return &CP->second;
56*9880d681SAndroid Build Coastguard Worker }
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker ConstantPool &
getOrCreateConstantPool(MCSection * Section)59*9880d681SAndroid Build Coastguard Worker AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
60*9880d681SAndroid Build Coastguard Worker   return ConstantPools[Section];
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker 
emitConstantPool(MCStreamer & Streamer,MCSection * Section,ConstantPool & CP)63*9880d681SAndroid Build Coastguard Worker static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
64*9880d681SAndroid Build Coastguard Worker                              ConstantPool &CP) {
65*9880d681SAndroid Build Coastguard Worker   if (!CP.empty()) {
66*9880d681SAndroid Build Coastguard Worker     Streamer.SwitchSection(Section);
67*9880d681SAndroid Build Coastguard Worker     CP.emitEntries(Streamer);
68*9880d681SAndroid Build Coastguard Worker   }
69*9880d681SAndroid Build Coastguard Worker }
70*9880d681SAndroid Build Coastguard Worker 
emitAll(MCStreamer & Streamer)71*9880d681SAndroid Build Coastguard Worker void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
72*9880d681SAndroid Build Coastguard Worker   // Dump contents of assembler constant pools.
73*9880d681SAndroid Build Coastguard Worker   for (auto &CPI : ConstantPools) {
74*9880d681SAndroid Build Coastguard Worker     MCSection *Section = CPI.first;
75*9880d681SAndroid Build Coastguard Worker     ConstantPool &CP = CPI.second;
76*9880d681SAndroid Build Coastguard Worker 
77*9880d681SAndroid Build Coastguard Worker     emitConstantPool(Streamer, Section, CP);
78*9880d681SAndroid Build Coastguard Worker   }
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker 
emitForCurrentSection(MCStreamer & Streamer)81*9880d681SAndroid Build Coastguard Worker void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
82*9880d681SAndroid Build Coastguard Worker   MCSection *Section = Streamer.getCurrentSection().first;
83*9880d681SAndroid Build Coastguard Worker   if (ConstantPool *CP = getConstantPool(Section)) {
84*9880d681SAndroid Build Coastguard Worker     emitConstantPool(Streamer, Section, *CP);
85*9880d681SAndroid Build Coastguard Worker   }
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker 
addEntry(MCStreamer & Streamer,const MCExpr * Expr,unsigned Size,SMLoc Loc)88*9880d681SAndroid Build Coastguard Worker const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
89*9880d681SAndroid Build Coastguard Worker                                                const MCExpr *Expr,
90*9880d681SAndroid Build Coastguard Worker                                                unsigned Size, SMLoc Loc) {
91*9880d681SAndroid Build Coastguard Worker   MCSection *Section = Streamer.getCurrentSection().first;
92*9880d681SAndroid Build Coastguard Worker   return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
93*9880d681SAndroid Build Coastguard Worker                                                    Size, Loc);
94*9880d681SAndroid Build Coastguard Worker }
95