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