Class MacroPool

java.lang.Object
mars.assembler.MacroPool

public class MacroPool extends Object
Stores information of macros defined by now.
Will be used in first pass of assembling MIPS source code. Upon reaching .macro directive, parser calls beginMacro(Token) and skips source code lines until it reaches .end_macro directive. Then calls commitMacro(Token) and the macro information stored in a Macro instance will be added to macroList.
Each MIPSprogram will have one MacroPool
NOTE: Forward referencing macros (macro expansion before its definition in source code) and Nested macro definition (defining a macro inside other macro definition) are not supported.
Author:
M.H.Sekhavat <sekhavat17@gmail.com>
  • Constructor Details

    • MacroPool

      public MacroPool(MIPSprogram mipsProgram)
      Create an empty MacroPool for given program
      Parameters:
      mipsProgram - associated MIPS program
  • Method Details

    • beginMacro

      public void beginMacro(Token nameToken)
      Called by parser upon reaching .macro directive.
      Instantiates a new Macro object and stores it in current . current will be added to macroList by commitMacro(Token)
      Parameters:
      nameToken - Token containing name of macro after .macro directive
    • commitMacro

      public void commitMacro(Token endToken)
      Called by parser upon reaching .end_macro directive.
      Adds/Replaces current macro into the macroList.
      Parameters:
      endToken - Token containing .end_macro directive in source code
    • getMatchingMacro

      public Macro getMatchingMacro(TokenList tokens, int callerLine)
      Called by parser upon reaching a macro expansion call
      Parameters:
      tokens - tokens passed to macro expansion call
      Returns:
      Macro object matching the name and argument count of tokens passed
    • matchesAnyMacroName

      public boolean matchesAnyMacroName(String value)
      Parameters:
      value -
      Returns:
      true if any macros have been defined with name value by now, not concerning arguments count.
    • getCurrent

      public Macro getCurrent()
    • setCurrent

      public void setCurrent(Macro current)
    • getNextCounter

      public int getNextCounter()
      counter will be set to 0 on construction of this class and will be incremented by each call. parser calls this method once for every expansion. it will be a unique id for each macro expansion in a file
      Returns:
      counter value
    • getCallStack

      public ArrayList<Integer> getCallStack()
    • pushOnCallStack

      public boolean pushOnCallStack(Token token)
      Adds the token's source line to the call stack
      Parameters:
      token -
      Returns:
      true if expansion loop is detected
    • popFromCallStack

      public void popFromCallStack()
    • getExpansionHistory

      public String getExpansionHistory()