Home | History | Annotate | Download | only in MC
      1 //===- ConstantPool.h - Keep track of assembler-generated  ------*- 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 declares the ConstantPool and AssemblerConstantPools classes.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 
     15 #ifndef LLVM_MC_CONSTANTPOOL_H
     16 #define LLVM_MC_CONSTANTPOOL_H
     17 
     18 #include "llvm/ADT/SmallVector.h"
     19 namespace llvm {
     20 class MCContext;
     21 class MCExpr;
     22 class MCSection;
     23 class MCStreamer;
     24 class MCSymbol;
     25 // A class to keep track of assembler-generated constant pools that are use to
     26 // implement the ldr-pseudo.
     27 class ConstantPool {
     28   typedef SmallVector<std::pair<MCSymbol *, const MCExpr *>, 4> EntryVecTy;
     29   EntryVecTy Entries;
     30 
     31 public:
     32   // Initialize a new empty constant pool
     33   ConstantPool() {}
     34 
     35   // Add a new entry to the constant pool in the next slot.
     36   // \param Value is the new entry to put in the constant pool.
     37   //
     38   // \returns a MCExpr that references the newly inserted value
     39   const MCExpr *addEntry(const MCExpr *Value, MCContext &Context);
     40 
     41   // Emit the contents of the constant pool using the provided streamer.
     42   void emitEntries(MCStreamer &Streamer);
     43 
     44   // Return true if the constant pool is empty
     45   bool empty();
     46 };
     47 
     48 class AssemblerConstantPools {
     49   // Map type used to keep track of per-Section constant pools used by the
     50   // ldr-pseudo opcode. The map associates a section to its constant pool. The
     51   // constant pool is a vector of (label, value) pairs. When the ldr
     52   // pseudo is parsed we insert a new (label, value) pair into the constant pool
     53   // for the current section and add MCSymbolRefExpr to the new label as
     54   // an opcode to the ldr. After we have parsed all the user input we
     55   // output the (label, value) pairs in each constant pool at the end of the
     56   // section.
     57   //
     58   // We use the MapVector for the map type to ensure stable iteration of
     59   // the sections at the end of the parse. We need to iterate over the
     60   // sections in a stable order to ensure that we have print the
     61   // constant pools in a deterministic order when printing an assembly
     62   // file.
     63   typedef MapVector<const MCSection *, ConstantPool> ConstantPoolMapTy;
     64   ConstantPoolMapTy ConstantPools;
     65 
     66 public:
     67   AssemblerConstantPools() {}
     68   ~AssemblerConstantPools() {}
     69 
     70   void emitAll(MCStreamer &Streamer);
     71   void emitForCurrentSection(MCStreamer &Streamer);
     72   const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr);
     73 
     74 private:
     75   ConstantPool *getConstantPool(const MCSection *Section);
     76   ConstantPool &getOrCreateConstantPool(const MCSection *Section);
     77 };
     78 } // end namespace llvm
     79 
     80 #endif
     81