blob: 56c47b224448e1c0e89dd69dfef6e28beebf14c8 [file] [log] [blame]
Reid Spencer293a55f2004-05-25 18:44:51 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
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//
10// This file implements a utility class for keeping track of slot numbers for
11// bytecode and assembly writing.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Internal/SlotTable.h"
16#include "llvm/Type.h"
17#include "llvm/Value.h"
18#include "llvm/GlobalValue.h"
19#include "llvm/Constants.h"
20
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// SlotTable Implementation
25//===----------------------------------------------------------------------===//
26
27SlotTable::SlotTable( bool dont_insert_primitives ) {
28 if ( ! dont_insert_primitives )
29 this->insertPrimitives();
30}
31
32// empty - determine if the slot table is completely empty.
33bool SlotTable::empty() const {
34 return vTable.empty() && vMap.empty() && tPlane.empty() && tMap.empty();
35}
36
37// getSlot - get the slot number associated with value Val
38SlotTable::SlotNum SlotTable::getSlot(const Value* Val) const {
39 ValueMap::const_iterator I = vMap.find( Val );
40 if ( I != vMap.end() )
41 return I->second;
42
43 // Do not number ConstantPointerRef's at all. They are an abomination.
44 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Val))
45 return this->getSlot(CPR->getValue());
46
47 return BAD_SLOT;
48}
49
50// getSlot - get the slot number associated with type Typ
51SlotTable::SlotNum SlotTable::getSlot(const Type* Typ) const {
52 TypeMap::const_iterator I = tMap.find( Typ );
53 if ( I != tMap.end() )
54 return I->second;
55
56 return BAD_SLOT;
57}
58
59// clear - completely clear the slot table of all entries
60void SlotTable::clear() {
61 vTable.clear();
62 vMap.clear();
63 tPlane.clear();
64 tMap.clear();
65}
66
67// resize - make sure there's enough room for specific number of planes
68void SlotTable::resize( size_t new_size ) {
69 vTable.resize( new_size );
70}
71
72// insert - insert a Value into a specific plane
73SlotTable::SlotNum SlotTable::insert( const Value* Val, PlaneNum plane ) {
74 if ( vTable.size() <= plane ) // Make sure we have the type plane allocated
75 vTable.resize(plane+1, ValuePlane());
76
77 // Insert node into table and map
78 SlotNum DestSlot = vMap[Val] = vTable[plane].size();
79 vTable[plane].push_back(Val);
80 return DestSlot;
81}
82
83// insert - insert a type into a specific plane
84SlotTable::SlotNum SlotTable::insert( const Type* Typ ) {
85 // Insert node into table and map
86 SlotNum DestSlot = tMap[Typ] = tPlane.size();
87 tPlane.push_back(Typ);
88 return DestSlot;
89}
90
91// remove - remove a value from the slot table
92SlotTable::SlotNum SlotTable::remove( const Value* Val, PlaneNum plane ) {
93 // FIXME: not implemented - not sure we need it
94 return BAD_SLOT;
95}
96
97// remove - remove a type from the slot table
98SlotTable::SlotNum SlotTable::remove( const Type* Typ ) {
99 // FIXME: not implemented - not sure we need it
100 return BAD_SLOT;
101}
102
103// insertPrimitives - insert the primitive types for initialization
104// Make sure that all of the primitive types are in the table
105// and that their Primitive ID is equal to their slot #
106void SlotTable::insertPrimitives() {
107 for (PlaneNum plane = 0; plane < Type::FirstDerivedTyID; ++plane) {
108 const Type* Ty = Type::getPrimitiveType((Type::PrimitiveID) plane);
109 assert(Ty && "Couldn't get primitive type id");
110 SlotNum slot = this->insert(Ty);
111 assert(slot == plane && "Type slot didn't match plane number");
112 }
113}
114
115// vim: sw=2