Reid Spencer | 3751bd6 | 2004-05-25 19:03:21 +0000 | [diff] [blame] | 1 | //===-- SlotTable.cpp - Abstract data type for slot numbers ---------------===// |
Reid Spencer | 293a55f | 2004-05-25 18:44:51 +0000 | [diff] [blame] | 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 | // |
Reid Spencer | 3751bd6 | 2004-05-25 19:03:21 +0000 | [diff] [blame] | 10 | // This file implements an abstract data type for keeping track of slot numbers |
| 11 | // for bytecode and assembly writing or any other purpose. |
Reid Spencer | 293a55f | 2004-05-25 18:44:51 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | 6269ded | 2004-06-09 04:38:34 +0000 | [diff] [blame^] | 15 | #include "SlotTable.h" |
Reid Spencer | 293a55f | 2004-05-25 18:44:51 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Reid Spencer | 3751bd6 | 2004-05-25 19:03:21 +0000 | [diff] [blame] | 17 | #include "llvm/Type.h" |
| 18 | #include "llvm/GlobalValue.h" |
Reid Spencer | 293a55f | 2004-05-25 18:44:51 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // SlotTable Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | SlotTable::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. |
| 32 | bool 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 |
| 37 | SlotTable::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 |
| 50 | SlotTable::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 |
| 59 | void 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 |
| 67 | void SlotTable::resize( size_t new_size ) { |
| 68 | vTable.resize( new_size ); |
| 69 | } |
| 70 | |
| 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()); |
| 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 Spencer | 3751bd6 | 2004-05-25 19:03:21 +0000 | [diff] [blame] | 82 | // insert - insert a type |
Reid Spencer | 293a55f | 2004-05-25 18:44:51 +0000 | [diff] [blame] | 83 | SlotTable::SlotNum SlotTable::insert( const Type* Typ ) { |
Reid Spencer | 3751bd6 | 2004-05-25 19:03:21 +0000 | [diff] [blame] | 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"); |
Reid Spencer | 293a55f | 2004-05-25 18:44:51 +0000 | [diff] [blame] | 87 | 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 |
| 93 | SlotTable::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 |
| 99 | SlotTable::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 # |
| 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"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // vim: sw=2 |