blob: d80515d137111c14ea1db6cc7932408405d9b8a3 [file] [log] [blame]
Chris Lattnerb5794002002-04-07 22:49:37 +00001//===-- llvm/SlotCalculator.h - Calculate value slots ------------*- C++ -*-==//
Chris Lattner00950542001-06-06 20:29:01 +00002//
Chris Lattnerb5794002002-04-07 22:49:37 +00003// 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.
Chris Lattner00950542001-06-06 20:29:01 +000010//
11//===----------------------------------------------------------------------===//
12
Chris Lattnerb5794002002-04-07 22:49:37 +000013#ifndef LLVM_SLOTCALCULATOR_H
14#define LLVM_SLOTCALCULATOR_H
Chris Lattner00950542001-06-06 20:29:01 +000015
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymTabValue.h"
17#include <vector>
18#include <map>
Chris Lattner644dc172001-07-14 06:08:51 +000019class Value;
Chris Lattner4c4007b2001-09-07 16:27:05 +000020class Module;
Chris Lattnere7506a32002-03-23 22:51:58 +000021class Function;
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattner4c4007b2001-09-07 16:27:05 +000023class SlotCalculator {
Chris Lattner00950542001-06-06 20:29:01 +000024 const Module *TheModule;
25 bool IgnoreNamedNodes; // Shall we not count named nodes?
26
Chris Lattner697954c2002-01-20 22:54:45 +000027 typedef std::vector<const Value*> TypePlane;
28 std::vector<TypePlane> Table;
29 std::map<const Value *, unsigned> NodeMap;
Chris Lattner00950542001-06-06 20:29:01 +000030
31 // ModuleLevel - Used to keep track of which values belong to the module,
Chris Lattnerb5794002002-04-07 22:49:37 +000032 // and which values belong to the currently incorporated function.
Chris Lattner00950542001-06-06 20:29:01 +000033 //
Chris Lattner697954c2002-01-20 22:54:45 +000034 std::vector<unsigned> ModuleLevel;
Chris Lattner00950542001-06-06 20:29:01 +000035
36public:
37 SlotCalculator(const Module *M, bool IgnoreNamed);
Chris Lattnere7506a32002-03-23 22:51:58 +000038 // Start out in incorp state
39 SlotCalculator(const Function *M, bool IgnoreNamed);
Chris Lattner00950542001-06-06 20:29:01 +000040 inline ~SlotCalculator() {}
41
42 // getValSlot returns < 0 on error!
43 int getValSlot(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
Chris Lattnerb5794002002-04-07 22:49:37 +000054 // If you'd like to deal with a function, use these two methods to get its
55 // data into the SlotCalculator!
Chris Lattner00950542001-06-06 20:29:01 +000056 //
Chris Lattnerb5794002002-04-07 22:49:37 +000057 void incorporateFunction(const Function *F);
58 void purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +000059
60protected:
Chris Lattner4c4007b2001-09-07 16:27:05 +000061 // insertVal - Insert a value into the value table... Return the slot that it
62 // occupies, or -1 if the declaration is to be ignored because of the
63 // IgnoreNamedNodes flag.
Chris Lattner00950542001-06-06 20:29:01 +000064 //
Chris Lattner4c4007b2001-09-07 16:27:05 +000065 int insertVal(const Value *D, bool dontIgnore = false);
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner4c4007b2001-09-07 16:27:05 +000067 // insertValue - Values can be crammed into here at will... if they haven't
68 // been inserted already, they get inserted, otherwise they are ignored.
Chris Lattner00950542001-06-06 20:29:01 +000069 //
Chris Lattner4c4007b2001-09-07 16:27:05 +000070 int insertValue(const Value *D);
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattner4c4007b2001-09-07 16:27:05 +000072 // doInsertVal - Small helper function to be called only be insertVal.
73 int doInsertVal(const Value *D);
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattnerb5794002002-04-07 22:49:37 +000075 // processModule - Process all of the module level function declarations and
Chris Lattner4c4007b2001-09-07 16:27:05 +000076 // types that are available.
Chris Lattner00950542001-06-06 20:29:01 +000077 //
Chris Lattner4c4007b2001-09-07 16:27:05 +000078 void processModule();
Chris Lattner00950542001-06-06 20:29:01 +000079
Chris Lattner4c4007b2001-09-07 16:27:05 +000080 // processSymbolTable - Insert all of the values in the specified symbol table
81 // into the values table...
Chris Lattner00950542001-06-06 20:29:01 +000082 //
Chris Lattner4c4007b2001-09-07 16:27:05 +000083 void processSymbolTable(const SymbolTable *ST);
84 void processSymbolTableConstants(const SymbolTable *ST);
Chris Lattner00950542001-06-06 20:29:01 +000085};
86
87#endif