mars.assembler
Class SymbolTable

java.lang.Object
  extended by mars.assembler.SymbolTable

public class SymbolTable
extends Object

Creats a table of Symbol objects.

Author:
Jason Bumgarner, Jason Shrewsbury

Field Summary
static int NOT_FOUND
           
 
Constructor Summary
SymbolTable(String filename)
          Create a new empty symbol table for given file
 
Method Summary
 void addSymbol(Token token, int address, boolean b, ErrorList errors)
          Adds a Symbol object into the array of Symbols.
 void clear()
          Creates a fresh arrayList for a new table.
 void fixSymbolTableAddress(int originalAddress, int replacementAddress)
          Fix address in symbol table entry.
 int getAddress(String s)
          Method to return the address associated with the given label.
 int getAddressLocalOrGlobal(String s)
          Method to return the address associated with the given label.
 ArrayList getAllSymbols()
          For obtaining all the Symbols.
 ArrayList getDataSymbols()
          For obtaining the Data Symbols.
 int getSize()
          Get the count of entries currently in the table.
static String getStartLabel()
          Fetches the text segment label (symbol) which, if declared global, indicates the starting address for execution.
 Symbol getSymbol(String s)
          Produce Symbol object from symbol table that corresponds to given String.
 Symbol getSymbolGivenAddress(String s)
          Produce Symbol object from symbol table that has the given address.
 Symbol getSymbolGivenAddressLocalOrGlobal(String s)
          Produce Symbol object from either local or global symbol table that has the given address.
 ArrayList getTextSymbols()
          For obtaining the Text Symbols.
 void removeSymbol(Token token)
          Removes a symbol from the Symbol table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOT_FOUND

public static final int NOT_FOUND
See Also:
Constant Field Values
Constructor Detail

SymbolTable

public SymbolTable(String filename)
Create a new empty symbol table for given file

Parameters:
filename - name of file this symbol table is associated with. Will be used only for output/display so it can be any descriptive string.
Method Detail

addSymbol

public void addSymbol(Token token,
                      int address,
                      boolean b,
                      ErrorList errors)
Adds a Symbol object into the array of Symbols.

Parameters:
token - The token representing the Symbol.
address - The address of the Symbol.
b - The type of Symbol, true for data, false for text.
errors - List to which to add any processing errors that occur.

removeSymbol

public void removeSymbol(Token token)
Removes a symbol from the Symbol table. If not found, it does nothing. This will rarely happen (only when variable is declared .globl after already being defined in the local symbol table).

Parameters:
token - The token representing the Symbol.

getAddress

public int getAddress(String s)
Method to return the address associated with the given label.

Parameters:
s - The label.
Returns:
The memory address of the label given, or NOT_FOUND if not found in symbol table.

getAddressLocalOrGlobal

public int getAddressLocalOrGlobal(String s)
Method to return the address associated with the given label. Look first in this (local) symbol table then in symbol table of labels declared global (.globl directive).

Parameters:
s - The label.
Returns:
The memory address of the label given, or NOT_FOUND if not found in symbol table.

getSymbol

public Symbol getSymbol(String s)
Produce Symbol object from symbol table that corresponds to given String.

Parameters:
s - target String
Returns:
Symbol object for requested target, null if not found in symbol table.

getSymbolGivenAddress

public Symbol getSymbolGivenAddress(String s)
Produce Symbol object from symbol table that has the given address.

Parameters:
s - String representing address
Returns:
Symbol object having requested address, null if address not found in symbol table.

getSymbolGivenAddressLocalOrGlobal

public Symbol getSymbolGivenAddressLocalOrGlobal(String s)
Produce Symbol object from either local or global symbol table that has the given address.

Parameters:
s - String representing address
Returns:
Symbol object having requested address, null if address not found in symbol table.

getDataSymbols

public ArrayList getDataSymbols()
For obtaining the Data Symbols.

Returns:
An ArrayList of Symbol objects.

getTextSymbols

public ArrayList getTextSymbols()
For obtaining the Text Symbols.

Returns:
An ArrayList of Symbol objects.

getAllSymbols

public ArrayList getAllSymbols()
For obtaining all the Symbols.

Returns:
An ArrayList of Symbol objects.

getSize

public int getSize()
Get the count of entries currently in the table.

Returns:
Number of symbol table entries.

clear

public void clear()
Creates a fresh arrayList for a new table.


fixSymbolTableAddress

public void fixSymbolTableAddress(int originalAddress,
                                  int replacementAddress)
Fix address in symbol table entry. Any and all entries that match the original address will be modified to contain the replacement address. There is no effect, if none of the addresses matches.

Parameters:
originalAddress - Address associated with 0 or more symtab entries.
replacementAddress - Any entry that has originalAddress will have its address updated to this value. Does nothing if none do.

getStartLabel

public static String getStartLabel()
Fetches the text segment label (symbol) which, if declared global, indicates the starting address for execution.

Returns:
String containing global label whose text segment address is starting address for program execution.