LLVM API Documentation

ConstantPools.cpp
Go to the documentation of this file.
00001 //===- ConstantPools.cpp - ConstantPool class --*- C++ -*---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the ConstantPool and  AssemblerConstantPools classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 #include "llvm/ADT/MapVector.h"
00014 #include "llvm/MC/MCContext.h"
00015 #include "llvm/MC/MCExpr.h"
00016 #include "llvm/MC/MCStreamer.h"
00017 #include "llvm/MC/ConstantPools.h"
00018 
00019 using namespace llvm;
00020 //
00021 // ConstantPool implementation
00022 //
00023 // Emit the contents of the constant pool using the provided streamer.
00024 void ConstantPool::emitEntries(MCStreamer &Streamer) {
00025   if (Entries.empty())
00026     return;
00027   Streamer.EmitDataRegion(MCDR_DataRegion);
00028   for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
00029        I != E; ++I) {
00030     Streamer.EmitCodeAlignment(I->Size); // align naturally
00031     Streamer.EmitLabel(I->Label);
00032     Streamer.EmitValue(I->Value, I->Size);
00033   }
00034   Streamer.EmitDataRegion(MCDR_DataRegionEnd);
00035   Entries.clear();
00036 }
00037 
00038 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
00039                                      unsigned Size) {
00040   MCSymbol *CPEntryLabel = Context.CreateTempSymbol();
00041 
00042   Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size));
00043   return MCSymbolRefExpr::Create(CPEntryLabel, Context);
00044 }
00045 
00046 bool ConstantPool::empty() { return Entries.empty(); }
00047 
00048 //
00049 // AssemblerConstantPools implementation
00050 //
00051 ConstantPool *
00052 AssemblerConstantPools::getConstantPool(const MCSection *Section) {
00053   ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
00054   if (CP == ConstantPools.end())
00055     return nullptr;
00056 
00057   return &CP->second;
00058 }
00059 
00060 ConstantPool &
00061 AssemblerConstantPools::getOrCreateConstantPool(const MCSection *Section) {
00062   return ConstantPools[Section];
00063 }
00064 
00065 static void emitConstantPool(MCStreamer &Streamer, const MCSection *Section,
00066                              ConstantPool &CP) {
00067   if (!CP.empty()) {
00068     Streamer.SwitchSection(Section);
00069     CP.emitEntries(Streamer);
00070   }
00071 }
00072 
00073 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
00074   // Dump contents of assembler constant pools.
00075   for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
00076                                    CPE = ConstantPools.end();
00077        CPI != CPE; ++CPI) {
00078     const MCSection *Section = CPI->first;
00079     ConstantPool &CP = CPI->second;
00080 
00081     emitConstantPool(Streamer, Section, CP);
00082   }
00083 }
00084 
00085 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
00086   const MCSection *Section = Streamer.getCurrentSection().first;
00087   if (ConstantPool *CP = getConstantPool(Section)) {
00088     emitConstantPool(Streamer, Section, *CP);
00089   }
00090 }
00091 
00092 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
00093                                                const MCExpr *Expr,
00094                                                unsigned Size) {
00095   const MCSection *Section = Streamer.getCurrentSection().first;
00096   return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
00097                                                    Size);
00098 }