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