3228ce260ff99c4894ccd52974920fb7d7d91618
[oota-llvm.git] / include / llvm / SlotCalculator.h
1 //===-- llvm/SlotCalculator.h - Calculate value slots -----------*- C++ -*-===//
2 //
3 // This class calculates the slots that values will land in.  This is useful for
4 // when writing bytecode or assembly out, because you have to know these things.
5 //
6 // Specifically, this class calculates the "type plane numbering" that you see
7 // for a function if you strip out all of the symbols in it.  For assembly
8 // writing, this is used when a symbol does not have a name.  For bytecode
9 // writing, this is always used, and the symbol table is added on later.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SLOTCALCULATOR_H
14 #define LLVM_SLOTCALCULATOR_H
15
16 #include <vector>
17 #include <map>
18 class Value;
19 class Module;
20 class Function;
21 class SymbolTable;
22
23 class SlotCalculator {
24   const Module *TheModule;
25   bool IgnoreNamedNodes;     // Shall we not count named nodes?
26
27   typedef std::vector<const Value*> TypePlane;
28   std::vector<TypePlane> Table;
29   std::map<const Value *, unsigned> NodeMap;
30
31   // ModuleLevel - Used to keep track of which values belong to the module,
32   // and which values belong to the currently incorporated function.
33   //
34   std::vector<unsigned> ModuleLevel;
35
36 public:
37   SlotCalculator(const Module *M, bool IgnoreNamed);
38   // Start out in incorp state
39   SlotCalculator(const Function *M, bool IgnoreNamed);
40   inline ~SlotCalculator() {}
41   
42   // getSlot returns < 0 on error!
43   int getSlot(const Value *D) const;
44
45   inline unsigned getNumPlanes() const { return Table.size(); }
46   inline unsigned getModuleLevel(unsigned Plane) const { 
47     return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; 
48   }
49
50   inline const TypePlane &getPlane(unsigned Plane) const { 
51     return Table[Plane]; 
52   }
53
54   // If you'd like to deal with a function, use these two methods to get its
55   // data into the SlotCalculator!
56   //
57   void incorporateFunction(const Function *F);
58   void purgeFunction();
59
60 protected:
61   // getOrCreateSlot - Values can be crammed into here at will... if
62   // they haven't been inserted already, they get inserted, otherwise
63   // they are ignored.
64   //
65   int getOrCreateSlot(const Value *D);
66
67   // insertValue - Insert a value into the value table... Return the
68   // slot that it occupies, or -1 if the declaration is to be ignored
69   // because of the IgnoreNamedNodes flag.
70   //
71   int insertValue(const Value *D, bool dontIgnore = false);
72
73   // doInsertValue - Small helper function to be called only be insertVal.
74   int doInsertValue(const Value *D);
75
76   // processModule - Process all of the module level function declarations and
77   // types that are available.
78   //
79   void processModule();
80
81   // processSymbolTable - Insert all of the values in the specified symbol table
82   // into the values table...
83   //
84   void processSymbolTable(const SymbolTable *ST);
85   void processSymbolTableConstants(const SymbolTable *ST);
86 };
87
88 #endif