|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectAbstractTable
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.
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 |
public static StringTable stringtable
public static IdTable idtable
public static IntTable inttable
private static int MAXSIZE
protected java.util.Vector tbl
Constructor Detail |
AbstractTable()
Method Detail |
protected abstract AbstractSymbol getNewSymbol(java.lang.String s, int len, int index)
public AbstractSymbol addString(java.lang.String s, int maxchars)
s
- the string to addmaxchars
- the length of the prefix
public AbstractSymbol addString(java.lang.String s)
s
- the string to add
public AbstractSymbol addInt(int i)
i
- the integer to add
public java.util.Enumeration getSymbols()
Enumeration
public AbstractSymbol lookup(int index)
index
- the index of the symbol
public AbstractSymbol lookup(java.lang.String s)
s
- the string representation of the symbol
public java.lang.String toString()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |