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); |
| 28 | for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end(); |
| 29 | I != E; ++I) { |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 30 | Streamer.EmitCodeAlignment(I->Size); // align naturally |
| 31 | Streamer.EmitLabel(I->Label); |
| 32 | Streamer.EmitValue(I->Value, I->Size); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 33 | } |
| 34 | Streamer.EmitDataRegion(MCDR_DataRegionEnd); |
| 35 | Entries.clear(); |
| 36 | } |
| 37 | |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 38 | const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, |
| 39 | unsigned Size) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 40 | MCSymbol *CPEntryLabel = Context.createTempSymbol(); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 41 | |
David Peixotto | b0b3e66 | 2014-07-18 16:41:58 +0000 | [diff] [blame] | 42 | Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size)); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 43 | return MCSymbolRefExpr::Create(CPEntryLabel, Context); |
| 44 | } |
| 45 | |
| 46 | bool ConstantPool::empty() { return Entries.empty(); } |
| 47 | |
| 48 | // |
| 49 | // AssemblerConstantPools implementation |
| 50 | // |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 51 | ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) { |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 52 | ConstantPoolMapTy::iterator CP = ConstantPools.find(Section); |
| 53 | if (CP == ConstantPools.end()) |
| 54 | return nullptr; |
| 55 | |
| 56 | return &CP->second; |
| 57 | } |
| 58 | |
| 59 | ConstantPool & |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 60 | AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) { |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 61 | return ConstantPools[Section]; |
| 62 | } |
| 63 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 64 | static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 65 | ConstantPool &CP) { |
| 66 | if (!CP.empty()) { |
| 67 | Streamer.SwitchSection(Section); |
| 68 | CP.emitEntries(Streamer); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void AssemblerConstantPools::emitAll(MCStreamer &Streamer) { |
| 73 | // Dump contents of assembler constant pools. |
| 74 | for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(), |
| 75 | CPE = ConstantPools.end(); |
| 76 | CPI != CPE; ++CPI) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 77 | MCSection *Section = CPI->first; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 78 | ConstantPool &CP = CPI->second; |
| 79 | |
| 80 | emitConstantPool(Streamer, Section, CP); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 85 | MCSection *Section = Streamer.getCurrentSection().first; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 86 | if (ConstantPool *CP = getConstantPool(Section)) { |
| 87 | emitConstantPool(Streamer, Section, *CP); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer, |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 92 | const MCExpr *Expr, |
| 93 | unsigned Size) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 94 | MCSection *Section = Streamer.getCurrentSection().first; |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 95 | return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(), |
| 96 | Size); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 97 | } |