1 //===-- SlotTable.cpp - Abstract data type for slot numbers ---------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements an abstract data type for keeping track of slot numbers
11 // for bytecode and assembly writing or any other purpose.
13 //===----------------------------------------------------------------------===//
15 #include "SlotTable.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Type.h"
18 #include "llvm/GlobalValue.h"
22 //===----------------------------------------------------------------------===//
23 // SlotTable Implementation
24 //===----------------------------------------------------------------------===//
26 SlotTable::SlotTable( bool dont_insert_primitives ) {
27 if ( ! dont_insert_primitives )
28 this->insertPrimitives();
31 // empty - determine if the slot table is completely empty.
32 bool SlotTable::empty() const {
33 return vTable.empty() && vMap.empty() && tPlane.empty() && tMap.empty();
36 // getSlot - get the slot number associated with value Val
37 SlotTable::SlotNum SlotTable::getSlot(const Value* Val) const {
38 ValueMap::const_iterator I = vMap.find( Val );
39 if ( I != vMap.end() )
42 // Do not number ConstantPointerRef's at all. They are an abomination.
43 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Val))
44 return this->getSlot(CPR->getValue());
49 // getSlot - get the slot number associated with type Typ
50 SlotTable::SlotNum SlotTable::getSlot(const Type* Typ) const {
51 TypeMap::const_iterator I = tMap.find( Typ );
52 if ( I != tMap.end() )
58 // clear - completely clear the slot table of all entries
59 void SlotTable::clear() {
66 // resize - make sure there's enough room for specific number of planes
67 void SlotTable::resize( size_t new_size ) {
68 vTable.resize( new_size );
71 // insert - insert a Value into a specific plane
72 SlotTable::SlotNum SlotTable::insert( const Value* Val, PlaneNum plane ) {
73 if ( vTable.size() <= plane ) // Make sure we have the type plane allocated
74 vTable.resize(plane+1, ValuePlane());
76 // Insert node into table and map
77 SlotNum DestSlot = vMap[Val] = vTable[plane].size();
78 vTable[plane].push_back(Val);
82 // insert - insert a type
83 SlotTable::SlotNum SlotTable::insert( const Type* Typ ) {
84 // Insert node into table and map making sure that
85 // the same type isn't inserted twice.
86 assert(tMap.find(Typ) == tMap.end() && "Can't insert a Type multiple times");
87 SlotNum DestSlot = tMap[Typ] = tPlane.size();
88 tPlane.push_back(Typ);
92 // remove - remove a value from the slot table
93 SlotTable::SlotNum SlotTable::remove( const Value* Val, PlaneNum plane ) {
94 // FIXME: not implemented - not sure we need it
98 // remove - remove a type from the slot table
99 SlotTable::SlotNum SlotTable::remove( const Type* Typ ) {
100 // FIXME: not implemented - not sure we need it
104 // insertPrimitives - insert the primitive types for initialization
105 // Make sure that all of the primitive types are in the table
106 // and that their Primitive ID is equal to their slot #
107 void SlotTable::insertPrimitives() {
108 for (PlaneNum plane = 0; plane < Type::FirstDerivedTyID; ++plane) {
109 const Type* Ty = Type::getPrimitiveType((Type::PrimitiveID) plane);
110 assert(Ty && "Couldn't get primitive type id");
111 SlotNum slot = this->insert(Ty);
112 assert(slot == plane && "Type slot didn't match plane number");