1c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//===- ConstantPool.h - Keep track of assembler-generated  ------*- C++ -*-===//
2c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//
3c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//                     The LLVM Compiler Infrastructure
4c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//
5c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines// This file is distributed under the University of Illinois Open Source
6c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines// License. See LICENSE.TXT for details.
7c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//
8c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//===----------------------------------------------------------------------===//
9c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//
10c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines// This file declares the ConstantPool and AssemblerConstantPools classes.
11c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//
12c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines//===----------------------------------------------------------------------===//
13c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
14c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
1537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#ifndef LLVM_MC_CONSTANTPOOLS_H
1637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#define LLVM_MC_CONSTANTPOOLS_H
17c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
1837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/ADT/MapVector.h"
19c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/ADT/SmallVector.h"
20f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include "llvm/Support/SMLoc.h"
2137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
22c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesnamespace llvm {
23c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass MCContext;
24c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass MCExpr;
25c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass MCSection;
26c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass MCStreamer;
27c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass MCSymbol;
2837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
2937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstruct ConstantPoolEntry {
30f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_)
31f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    : Label(L), Value(Val), Size(Sz), Loc(Loc_) {}
3237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  MCSymbol *Label;
3337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const MCExpr *Value;
3437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  unsigned Size;
35f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  SMLoc Loc;
3637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines};
3737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
38c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines// A class to keep track of assembler-generated constant pools that are use to
39c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines// implement the ldr-pseudo.
40c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass ConstantPool {
4137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  typedef SmallVector<ConstantPoolEntry, 4> EntryVecTy;
42c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  EntryVecTy Entries;
43c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
44c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinespublic:
45c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // Initialize a new empty constant pool
46c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  ConstantPool() {}
47c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
48c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // Add a new entry to the constant pool in the next slot.
49c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // \param Value is the new entry to put in the constant pool.
5037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // \param Size is the size in bytes of the entry
51c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  //
52c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // \returns a MCExpr that references the newly inserted value
5337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const MCExpr *addEntry(const MCExpr *Value, MCContext &Context,
54f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                         unsigned Size, SMLoc Loc);
55c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
56c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // Emit the contents of the constant pool using the provided streamer.
57c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  void emitEntries(MCStreamer &Streamer);
58c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
59c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // Return true if the constant pool is empty
60c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  bool empty();
61c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines};
62c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
63c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesclass AssemblerConstantPools {
64c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // Map type used to keep track of per-Section constant pools used by the
65c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // ldr-pseudo opcode. The map associates a section to its constant pool. The
66c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // constant pool is a vector of (label, value) pairs. When the ldr
67c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // pseudo is parsed we insert a new (label, value) pair into the constant pool
68c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // for the current section and add MCSymbolRefExpr to the new label as
69c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // an opcode to the ldr. After we have parsed all the user input we
70c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // output the (label, value) pairs in each constant pool at the end of the
71c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // section.
72c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  //
73c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // We use the MapVector for the map type to ensure stable iteration of
74c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // the sections at the end of the parse. We need to iterate over the
75c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // sections in a stable order to ensure that we have print the
76c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // constant pools in a deterministic order when printing an assembly
77c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  // file.
786948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  typedef MapVector<MCSection *, ConstantPool> ConstantPoolMapTy;
79c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  ConstantPoolMapTy ConstantPools;
80c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
81c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinespublic:
82c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  void emitAll(MCStreamer &Streamer);
83c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  void emitForCurrentSection(MCStreamer &Streamer);
8437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
85f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                         unsigned Size, SMLoc Loc);
86c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
87c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesprivate:
886948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  ConstantPool *getConstantPool(MCSection *Section);
896948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  ConstantPool &getOrCreateConstantPool(MCSection *Section);
90c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines};
91c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines} // end namespace llvm
92c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
93c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#endif
94