820099273b7348d62de728006880341c0a958a5d
[oota-llvm.git] / lib / Bytecode / Writer / SlotCalculator.h
1 //===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class calculates the slots that values will land in.  This is useful for
11 // when writing bytecode or assembly out, because you have to know these things.
12 //
13 // Specifically, this class calculates the "type plane numbering" that you see
14 // for a function if you strip out all of the symbols in it.  For assembly
15 // writing, this is used when a symbol does not have a name.  For bytecode
16 // writing, this is always used, and the symbol table is added on later.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
21 #define LLVM_ANALYSIS_SLOTCALCULATOR_H
22
23 #include <vector>
24 #include <map>
25
26 namespace llvm {
27
28 class Value;
29 class Type;
30 class Module;
31 class Function;
32 class SymbolTable;
33 class TypeSymbolTable;
34 class ConstantArray;
35
36 class SlotCalculator {
37   const Module *TheModule;
38
39   typedef std::vector<const Type*> TypeList;
40   typedef std::vector<const Value*> TypePlane;
41   std::vector<TypePlane> Table;
42   TypeList Types;
43   typedef std::map<const Value*, unsigned> NodeMapType;
44   NodeMapType NodeMap;
45
46   typedef std::map<const Type*, unsigned> TypeMapType;
47   TypeMapType TypeMap;
48
49   /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
50   /// of all of the constants strings that need to be emitted.
51   std::vector<const ConstantArray*> ConstantStrings;
52
53   /// ModuleLevel - Used to keep track of which values belong to the module,
54   /// and which values belong to the currently incorporated function.
55   ///
56   std::vector<unsigned> ModuleLevel;
57   unsigned ModuleTypeLevel;
58
59   /// ModuleContainsAllFunctionConstants - This flag is set to true if all
60   /// function constants are incorporated into the module constant table.  This
61   /// is only possible if building information for a bytecode file.
62   bool ModuleContainsAllFunctionConstants;
63
64   SlotCalculator(const SlotCalculator &);  // DO NOT IMPLEMENT
65   void operator=(const SlotCalculator &);  // DO NOT IMPLEMENT
66 public:
67   SlotCalculator(const Module *M);
68   // Start out in incorp state
69   SlotCalculator(const Function *F);
70
71   /// getSlot - Return the slot number of the specified value in it's type
72   /// plane.  This returns < 0 on error!
73   ///
74   int getSlot(const Value *V) const;
75   int getSlot(const Type* T) const;
76
77   inline unsigned getNumPlanes() const { return Table.size(); }
78   inline unsigned getNumTypes() const { return Types.size(); }
79
80   inline unsigned getModuleLevel(unsigned Plane) const {
81     return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
82   }
83
84   /// Returns the number of types in the type list that are at module level
85   inline unsigned getModuleTypeLevel() const {
86     return ModuleTypeLevel;
87   }
88
89   TypePlane &getPlane(unsigned Plane);
90   TypeList& getTypes() { return Types; }
91
92   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
93   /// use these two methods to get its data into the SlotCalculator!
94   ///
95   void incorporateFunction(const Function *F);
96   void purgeFunction();
97
98   /// string_iterator/string_begin/end - Access the list of module-level
99   /// constant strings that have been incorporated.  This is only applicable to
100   /// bytecode files.
101   typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
102   string_iterator string_begin() const { return ConstantStrings.begin(); }
103   string_iterator string_end() const   { return ConstantStrings.end(); }
104
105 private:
106   // getOrCreateSlot - Values can be crammed into here at will... if
107   // they haven't been inserted already, they get inserted, otherwise
108   // they are ignored.
109   //
110   int getOrCreateSlot(const Value *V);
111   int getOrCreateSlot(const Type *T);
112
113   // insertValue - Insert a value into the value table... Return the
114   // slot that it occupies, or -1 if the declaration is to be ignored
115   // because of the IgnoreNamedNodes flag.
116   //
117   int insertValue(const Value *D, bool dontIgnore = false);
118   int insertType(const Type *T, bool dontIgnore = false);
119
120   // doInsertValue - Small helper function to be called only be insertVal.
121   int doInsertValue(const Value *V);
122   int doInsertType(const Type *T);
123
124   // processModule - Process all of the module level function declarations and
125   // types that are available.
126   //
127   void processModule();
128
129   // processSymbolTable - Insert all of the values in the specified symbol table
130   // into the values table...
131   //
132   void processTypeSymbolTable(const TypeSymbolTable *ST);
133   void processValueSymbolTable(const SymbolTable *ST);
134   void processSymbolTableConstants(const SymbolTable *ST);
135
136   // insertPrimitives - helper for constructors to insert primitive types.
137   void insertPrimitives();
138 };
139
140 } // End llvm namespace
141
142 #endif