Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 1 | //===- ConstantPools.cpp - ConstantPool class --*- C++ -*---------===// |
| 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 | #include "llvm/ADT/MapVector.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 14 | #include "llvm/MC/ConstantPools.h" |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCContext.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCStreamer.h" |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | // |
| 21 | // ConstantPool implementation |
| 22 | // |
| 23 | // Emit the contents of the constant pool using the provided streamer. |
| 24 | void ConstantPool::emitEntries(MCStreamer &Streamer) { |
| 25 | if (Entries.empty()) |
| 26 | return; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 27 | Streamer.EmitDataRegion(MCDR_DataRegion); |
Benjamin Kramer | 7b4658f | 2016-06-26 14:49:00 +0000 | [diff] [blame] | 28 | for (const ConstantPoolEntry &Entry : Entries) { |
| 29 | Streamer.EmitCodeAlignment(Entry.Size); // align naturally |
| 30 | Streamer.EmitLabel(Entry.Label); |
| 31 | Streamer.EmitValue(Entry.Value, Entry.Size, Entry.Loc); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 32 | } |
| 33 | Streamer.EmitDataRegion(MCDR_DataRegionEnd); |
| 34 | Entries.clear(); |
| 35 | } |
| 36 | |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 37 | const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 38 | unsigned Size, SMLoc Loc) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 39 | MCSymbol *CPEntryLabel = Context.createTempSymbol(); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 40 | |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 41 | Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 42 | return MCSymbolRefExpr::create(CPEntryLabel, Context); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool ConstantPool::empty() { return Entries.empty(); } |
| 46 | |
| 47 | // |
| 48 | // AssemblerConstantPools implementation |
| 49 | // |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 50 | ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) { |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 51 | ConstantPoolMapTy::iterator CP = ConstantPools.find(Section); |
| 52 | if (CP == ConstantPools.end()) |
| 53 | return nullptr; |
| 54 | |
| 55 | return &CP->second; |
| 56 | } |
| 57 | |
| 58 | ConstantPool & |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 59 | AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) { |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 60 | return ConstantPools[Section]; |
| 61 | } |
| 62 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 63 | static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 64 | ConstantPool &CP) { |
| 65 | if (!CP.empty()) { |
| 66 | Streamer.SwitchSection(Section); |
| 67 | CP.emitEntries(Streamer); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void AssemblerConstantPools::emitAll(MCStreamer &Streamer) { |
| 72 | // Dump contents of assembler constant pools. |
Benjamin Kramer | 7b4658f | 2016-06-26 14:49:00 +0000 | [diff] [blame] | 73 | for (auto &CPI : ConstantPools) { |
| 74 | MCSection *Section = CPI.first; |
| 75 | ConstantPool &CP = CPI.second; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 76 | |
| 77 | emitConstantPool(Streamer, Section, CP); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 82 | MCSection *Section = Streamer.getCurrentSection().first; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 83 | if (ConstantPool *CP = getConstantPool(Section)) { |
| 84 | emitConstantPool(Streamer, Section, *CP); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer, |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 89 | const MCExpr *Expr, |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 90 | unsigned Size, SMLoc Loc) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 91 | MCSection *Section = Streamer.getCurrentSection().first; |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 92 | return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(), |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 93 | Size, Loc); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 94 | } |