LLVM API Documentation

ConstantPools.h
Go to the documentation of this file.
00001 //===- ConstantPool.h - Keep track of assembler-generated  ------*- 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 declares the ConstantPool and AssemblerConstantPools classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 
00015 #ifndef LLVM_MC_CONSTANTPOOLS_H
00016 #define LLVM_MC_CONSTANTPOOLS_H
00017 
00018 #include "llvm/ADT/SmallVector.h"
00019 namespace llvm {
00020 class MCContext;
00021 class MCExpr;
00022 class MCSection;
00023 class MCStreamer;
00024 class MCSymbol;
00025 
00026 struct ConstantPoolEntry {
00027   ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz)
00028     : Label(L), Value(Val), Size(Sz) {}
00029   MCSymbol *Label;
00030   const MCExpr *Value;
00031   unsigned Size;
00032 };
00033 
00034 // A class to keep track of assembler-generated constant pools that are use to
00035 // implement the ldr-pseudo.
00036 class ConstantPool {
00037   typedef SmallVector<ConstantPoolEntry, 4> EntryVecTy;
00038   EntryVecTy Entries;
00039 
00040 public:
00041   // Initialize a new empty constant pool
00042   ConstantPool() {}
00043 
00044   // Add a new entry to the constant pool in the next slot.
00045   // \param Value is the new entry to put in the constant pool.
00046   // \param Size is the size in bytes of the entry
00047   //
00048   // \returns a MCExpr that references the newly inserted value
00049   const MCExpr *addEntry(const MCExpr *Value, MCContext &Context,
00050                          unsigned Size);
00051 
00052   // Emit the contents of the constant pool using the provided streamer.
00053   void emitEntries(MCStreamer &Streamer);
00054 
00055   // Return true if the constant pool is empty
00056   bool empty();
00057 };
00058 
00059 class AssemblerConstantPools {
00060   // Map type used to keep track of per-Section constant pools used by the
00061   // ldr-pseudo opcode. The map associates a section to its constant pool. The
00062   // constant pool is a vector of (label, value) pairs. When the ldr
00063   // pseudo is parsed we insert a new (label, value) pair into the constant pool
00064   // for the current section and add MCSymbolRefExpr to the new label as
00065   // an opcode to the ldr. After we have parsed all the user input we
00066   // output the (label, value) pairs in each constant pool at the end of the
00067   // section.
00068   //
00069   // We use the MapVector for the map type to ensure stable iteration of
00070   // the sections at the end of the parse. We need to iterate over the
00071   // sections in a stable order to ensure that we have print the
00072   // constant pools in a deterministic order when printing an assembly
00073   // file.
00074   typedef MapVector<const MCSection *, ConstantPool> ConstantPoolMapTy;
00075   ConstantPoolMapTy ConstantPools;
00076 
00077 public:
00078   AssemblerConstantPools() {}
00079   ~AssemblerConstantPools() {}
00080 
00081   void emitAll(MCStreamer &Streamer);
00082   void emitForCurrentSection(MCStreamer &Streamer);
00083   const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
00084                          unsigned Size);
00085 
00086 private:
00087   ConstantPool *getConstantPool(const MCSection *Section);
00088   ConstantPool &getOrCreateConstantPool(const MCSection *Section);
00089 };
00090 } // end namespace llvm
00091 
00092 #endif