MC: support different sized constants in constant pools
[oota-llvm.git] / include / llvm / MC / ConstantPools.h
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
26 struct ConstantPoolEntry {
27   MCSymbol *Label;
28   const MCExpr *Value;
29   unsigned Size;
30 };
31
32 // A class to keep track of assembler-generated constant pools that are use to
33 // implement the ldr-pseudo.
34 class ConstantPool {
35   typedef SmallVector<ConstantPoolEntry, 4> EntryVecTy;
36   EntryVecTy Entries;
37
38 public:
39   // Initialize a new empty constant pool
40   ConstantPool() {}
41
42   // Add a new entry to the constant pool in the next slot.
43   // \param Value is the new entry to put in the constant pool.
44   // \param Size is the size in bytes of the entry
45   //
46   // \returns a MCExpr that references the newly inserted value
47   const MCExpr *addEntry(const MCExpr *Value, MCContext &Context,
48                          unsigned Size);
49
50   // Emit the contents of the constant pool using the provided streamer.
51   void emitEntries(MCStreamer &Streamer);
52
53   // Return true if the constant pool is empty
54   bool empty();
55 };
56
57 class AssemblerConstantPools {
58   // Map type used to keep track of per-Section constant pools used by the
59   // ldr-pseudo opcode. The map associates a section to its constant pool. The
60   // constant pool is a vector of (label, value) pairs. When the ldr
61   // pseudo is parsed we insert a new (label, value) pair into the constant pool
62   // for the current section and add MCSymbolRefExpr to the new label as
63   // an opcode to the ldr. After we have parsed all the user input we
64   // output the (label, value) pairs in each constant pool at the end of the
65   // section.
66   //
67   // We use the MapVector for the map type to ensure stable iteration of
68   // the sections at the end of the parse. We need to iterate over the
69   // sections in a stable order to ensure that we have print the
70   // constant pools in a deterministic order when printing an assembly
71   // file.
72   typedef MapVector<const MCSection *, ConstantPool> ConstantPoolMapTy;
73   ConstantPoolMapTy ConstantPools;
74
75 public:
76   AssemblerConstantPools() {}
77   ~AssemblerConstantPools() {}
78
79   void emitAll(MCStreamer &Streamer);
80   void emitForCurrentSection(MCStreamer &Streamer);
81   const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
82                          unsigned Size);
83
84 private:
85   ConstantPool *getConstantPool(const MCSection *Section);
86   ConstantPool &getOrCreateConstantPool(const MCSection *Section);
87 };
88 } // end namespace llvm
89
90 #endif