Class SymbolTable

java.lang.Object
mars.assembler.SymbolTable

public class SymbolTable extends Object
Creates a table of Symbol objects.
Version:
June 2003
Author:
Jason Bumgarner, Jason Shrewsbury
  • Field Details

  • Constructor Details

    • 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 Details

    • 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<Symbol> getDataSymbols()
      For obtaining the Data Symbols.
      Returns:
      An ArrayList of Symbol objects.
    • getTextSymbols

      public ArrayList<Symbol> getTextSymbols()
      For obtaining the Text Symbols.
      Returns:
      An ArrayList of Symbol objects.
    • getAllSymbols

      public ArrayList<Symbol> 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.