blob: 358a6ea21e0b6df20cccb168c78d36f40b941985 [file] [log] [blame]
Reid Spencer3751bd62004-05-25 19:03:21 +00001//===-- SlotTable.cpp - Abstract data type for slot numbers ---------------===//
Reid Spencer293a55f2004-05-25 18:44:51 +00002//
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//
Reid Spencer3751bd62004-05-25 19:03:21 +000010// This file implements an abstract data type for keeping track of slot numbers
11// for bytecode and assembly writing or any other purpose.
Reid Spencer293a55f2004-05-25 18:44:51 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Spencer6269ded2004-06-09 04:38:34 +000015#include "SlotTable.h"
Reid Spencer293a55f2004-05-25 18:44:51 +000016#include "llvm/Constants.h"
Reid Spencer3751bd62004-05-25 19:03:21 +000017#include "llvm/Type.h"
18#include "llvm/GlobalValue.h"
Reid Spencer293a55f2004-05-25 18:44:51 +000019
20using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// SlotTable Implementation
24//===----------------------------------------------------------------------===//
25
26SlotTable::SlotTable( bool dont_insert_primitives ) {
27 if ( ! dont_insert_primitives )
28 this->insertPrimitives();
29}
30
31// empty - determine if the slot table is completely empty.
32bool SlotTable::empty() const {
33 return vTable.empty() && vMap.empty() && tPlane.empty() && tMap.empty();
34}
35
36// getSlot - get the slot number associated with value Val
37SlotTable::SlotNum SlotTable::getSlot(const Value* Val) const {
38 ValueMap::const_iterator I = vMap.find( Val );
39 if ( I != vMap.end() )
40 return I->second;
41
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());
45
46 return BAD_SLOT;
47}
48
49// getSlot - get the slot number associated with type Typ
50SlotTable::SlotNum SlotTable::getSlot(const Type* Typ) const {
51 TypeMap::const_iterator I = tMap.find( Typ );
52 if ( I != tMap.end() )
53 return I->second;
54
55 return BAD_SLOT;
56}
57
58// clear - completely clear the slot table of all entries
59void SlotTable::clear() {
60 vTable.clear();
61 vMap.clear();
62 tPlane.clear();
63 tMap.clear();
64}
65
66// resize - make sure there's enough room for specific number of planes
67void SlotTable::resize( size_t new_size ) {
68 vTable.resize( new_size );
69}
70
71// insert - insert a Value into a specific plane
72SlotTable::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());
75
76 // Insert node into table and map
77 SlotNum DestSlot = vMap[Val] = vTable[plane].size();
78 vTable[plane].push_back(Val);
79 return DestSlot;
80}
81
Reid Spencer3751bd62004-05-25 19:03:21 +000082// insert - insert a type
Reid Spencer293a55f2004-05-25 18:44:51 +000083SlotTable::SlotNum SlotTable::insert( const Type* Typ ) {
Reid Spencer3751bd62004-05-25 19:03:21 +000084 // 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");
Reid Spencer293a55f2004-05-25 18:44:51 +000087 SlotNum DestSlot = tMap[Typ] = tPlane.size();
88 tPlane.push_back(Typ);
89 return DestSlot;
90}
91
92// remove - remove a value from the slot table
93SlotTable::SlotNum SlotTable::remove( const Value* Val, PlaneNum plane ) {
94 // FIXME: not implemented - not sure we need it
95 return BAD_SLOT;
96}
97
98// remove - remove a type from the slot table
99SlotTable::SlotNum SlotTable::remove( const Type* Typ ) {
100 // FIXME: not implemented - not sure we need it
101 return BAD_SLOT;
102}
103
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 #
107void 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");
113 }
114}
115
116// vim: sw=2