Class AbstractTable

java.lang.Object
  extended byAbstractTable
Direct Known Subclasses:
IdTable, IntTable, StringTable

abstract class AbstractTable
extends java.lang.Object

Abstract string table implementation.

All compilers manage large numbers of strings such as program identifiers, numerical constants, and string constants. Often, many of these strings are the same. For example, each identifier typically occurs many times in a program. To ensure that string constants are stored compactly and manipulated efficiently, a specialized data structure, the string table, is employed.

A string table is a lookup table that maintains a single copy of each string. The Cool string table class provides methods for inserting and querying string tables in a variety of ways. While production compilers use hashed data structures to implement string tables, the Cool string tables are implemented as vectors. The components of Cool string tables are of type AbstractSymbol). Each AbstractSymbol stores a string, and an integer index unique to the string.

An important point about the structure of the Cool compiler is that there are actually three distinct string tables: one for string constants (AbstractTable.stringtable), one for integer constants (AbstractTable.inttable), and one for identifiers (AbstractTable.idtable). The code generator must distinguish integer constants and string constants from each other and from identifiers, because special code is produced for each string constant and each integer constant in the program. Having three distinct string tables makes this distinction easy. Note that each of the three tables has a different element type (StringSymbol, IntSymbol, and IdSymbol), each of which is a derived class of AbstractSymbol.

Because string tables store only one copy of each string, comparing whether two IntSymbols, StrSymbols, or IdSymbols x and y represent the same string can be done simply by comparing the two references x == y. Note that it does not make sense to compare entries from different string tables (e.g., IdSymbols with StringSymbols as these are guaranteed to be different even if the strings are the same.

See Also:
AbstractSymbol, StringSymbol, IdSymbol, IntSymbol

Field Summary
static IdTable idtable
          Global string table of identifiers
static IntTable inttable
          Global string table of integer constants
private static int MAXSIZE
           
static StringTable stringtable
          Global string table of string constants
protected  java.util.Vector tbl
          Vector of table entries
 
Constructor Summary
(package private) AbstractTable()
           
 
Method Summary
 AbstractSymbol addInt(int i)
          Adds the string representation of the specified integer to this string table
 AbstractSymbol addString(java.lang.String s)
          Adds the specified string to this string table
 AbstractSymbol addString(java.lang.String s, int maxchars)
          Adds prefix of the specified length to this string table
protected abstract  AbstractSymbol getNewSymbol(java.lang.String s, int len, int index)
          Creates a new symbol of the appropriate type
 java.util.Enumeration getSymbols()
          Returns an enumeration of symbols in this string table
 AbstractSymbol lookup(int index)
          Looks up a symbol in this string table by its index A fatal error is signalled if the index is out of bounds.
 AbstractSymbol lookup(java.lang.String s)
          Looks up a symbol in this string table by its string representation A fatal error is signalled if the string is not in the table
 java.lang.String toString()
          Produces a printable representation of the string table
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

stringtable

public static StringTable stringtable
Global string table of string constants


idtable

public static IdTable idtable
Global string table of identifiers


inttable

public static IntTable inttable
Global string table of integer constants


MAXSIZE

private static int MAXSIZE

tbl

protected java.util.Vector tbl
Vector of table entries

Constructor Detail

AbstractTable

AbstractTable()
Method Detail

getNewSymbol

protected abstract AbstractSymbol getNewSymbol(java.lang.String s,
                                               int len,
                                               int index)
Creates a new symbol of the appropriate type


addString

public AbstractSymbol addString(java.lang.String s,
                                int maxchars)
Adds prefix of the specified length to this string table

Parameters:
s - the string to add
maxchars - the length of the prefix
Returns:
the symbol for the string s

addString

public AbstractSymbol addString(java.lang.String s)
Adds the specified string to this string table

Parameters:
s - the string to add
Returns:
the symbol for the string s

addInt

public AbstractSymbol addInt(int i)
Adds the string representation of the specified integer to this string table

Parameters:
i - the integer to add
Returns:
the symbol for the integer i

getSymbols

public java.util.Enumeration getSymbols()
Returns an enumeration of symbols in this string table

Returns:
an enumeration of symbols
See Also:
Enumeration

lookup

public AbstractSymbol lookup(int index)
Looks up a symbol in this string table by its index A fatal error is signalled if the index is out of bounds.

Parameters:
index - the index of the symbol
Returns:
a symbol corresponding to the index

lookup

public AbstractSymbol lookup(java.lang.String s)
Looks up a symbol in this string table by its string representation A fatal error is signalled if the string is not in the table

Parameters:
s - the string representation of the symbol
Returns:
a symbol corresponding to the string

toString

public java.lang.String toString()
Produces a printable representation of the string table