Class AbstractStatementBuilder

java.lang.Object
edu.iu.jrsalata.AbstractStatementBuilder
Direct Known Subclasses:
SicStatementBuilder, StatementBuilder

public abstract class AbstractStatementBuilder extends Object
The AbstractStatementBuilder is an abstract class that handles most of the logic when it comes to creating concrete StatementBuilders Since most of the logic regarding assembler directives is the same, most of it is stored in this class.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected ArrayList<String>
    absoluteExpressions are expressions based on two variable locations Ex) BUFFEND - BUFFER would be absolute since this specifies BUFFEND - 10 would not be considered to be an absolute expression
    protected String
    Current name of the program block
    protected Queue<String>
    externalDefinitions refers to any label that may be accessed in a separate control section
    protected Queue<String>
    externalReferences refers to labels in this control section that are accessed in a separate section
    protected final HashMap<String,Format>
    formatTable is a map that holds the format of each instruction
    protected final HashMap<String,HexNum>
    instructionTable is a map that holds each of the assembler instructions
    protected String
    Current line the builder is processing
    protected int
    Current line number based on given input May be inaccurate due to how comments are handled
    literals are values that are hard coded in like =X'05' These need to be placed in a literal pool later, hence why they are stored here
    protected final HashMap<String,HexNum>
    locctrTable holds the location counter of each program block
    protected String
    Name of the given program
    protected ArrayList<String>
    with every external reference, we need a new modification record in order to specify where to find the external reference
    protected final HashMap<String,HexNum>
    registerTable is a map that holds each valid register
    protected final LinkedHashMap<String,HexNum>
    startTable handles the starting location of each program block Note that we are making startTable a LinkedHashMap this is so that we can maintain the order of each program block which is needed for calculating the relative start locations of each block
    protected Queue<Statement>
    statements is the queue that is returned to the client Each of them contains a statement that has appropriate info on its location, args, and values so it can be assembled
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Default constructor that initializes each variable
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    addLocctr(HexNum locctr)
    Adds a given value to the location counter (locctr) of the current block.
    protected void
    addLocctr(String block, HexNum locctr)
    Adds the specified location counter (locctr) value to the current location counter for the given block in the locctrTable.
    protected void
    Adds a statement to the list of statements.
    protected void
    Assembles literals by processing each unique literal from the literals queue.
    protected String
    Evaluates a mathematical expression represented as a string.
    Retrieves the queue of external definitions.
    Retrieves the queue of external references.
    Retrieves the location counter (LOCCTR) for the current block.
    Retrieves the location counter (LOCCTR) for the specified block.
    Retrieves the name, ensuring it is exactly six characters long.
    Retrieves the list of reference modifications.
    Retrieves the starting address as a HexNum for the default starting block
    Retrieves the starting HexNum for the specified block.
    Calculates the length and relative start of each block in the program, modifies the value of each symbol to be relative to the start of the program, and returns the queue of statements.
    Calculates the total length of all program blocks by summing up the location counters of each block in the start table.
    handleAsmStatement(String mnemonic, String args)
    Handles assembly statements by processing the given mnemonic and arguments.
    protected void
    Handles the BYTE directive in an assembly file.
    protected HexNum
    Handles the evaluation of an expression, replacing symbols with their decimal values, and determining if the expression is absolute or relative.
    protected void
    handleLabels(String label, String mnemonic, String args)
    Handles the processing of labels in the assembly code.
    protected void
    Handles a literal in the assembly code.
    protected String
    handleModification(String copyArgs, String part)
    Handles the modification of an external reference in the assembly code.
    protected final void
    Loads the instruction set from a specified file and populates the instruction and format tables.
    protected final void
    Loads the registers from the specified file and adds them to the register table.
    abstract void
    Processes a given assembly statement.
    void
    Sets the name for this program.
    protected void
    setStart(String block, HexNum start)
    Sets the starting address for a given block in the start table.
    protected String[]
    Splits an assembly statement into its constituent parts: mnemonic, arguments, and label.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • name

      protected String name
      Name of the given program
    • block

      protected String block
      Current name of the program block
    • line

      protected String line
      Current line the builder is processing
    • lineNum

      protected int lineNum
      Current line number based on given input May be inaccurate due to how comments are handled
    • absoluteExpressions

      protected ArrayList<String> absoluteExpressions
      absoluteExpressions are expressions based on two variable locations Ex) BUFFEND - BUFFER would be absolute since this specifies BUFFEND - 10 would not be considered to be an absolute expression
    • externalDefinitions

      protected Queue<String> externalDefinitions
      externalDefinitions refers to any label that may be accessed in a separate control section
    • externalReferences

      protected Queue<String> externalReferences
      externalReferences refers to labels in this control section that are accessed in a separate section
    • referenceModifications

      protected ArrayList<String> referenceModifications
      with every external reference, we need a new modification record in order to specify where to find the external reference
    • literals

      protected Queue<DirectiveStatement> literals
      literals are values that are hard coded in like =X'05' These need to be placed in a literal pool later, hence why they are stored here
    • statements

      protected Queue<Statement> statements
      statements is the queue that is returned to the client Each of them contains a statement that has appropriate info on its location, args, and values so it can be assembled
    • instructionTable

      protected final HashMap<String,HexNum> instructionTable
      instructionTable is a map that holds each of the assembler instructions
    • formatTable

      protected final HashMap<String,Format> formatTable
      formatTable is a map that holds the format of each instruction
    • registerTable

      protected final HashMap<String,HexNum> registerTable
      registerTable is a map that holds each valid register
    • locctrTable

      protected final HashMap<String,HexNum> locctrTable
      locctrTable holds the location counter of each program block
    • startTable

      protected final LinkedHashMap<String,HexNum> startTable
      startTable handles the starting location of each program block Note that we are making startTable a LinkedHashMap this is so that we can maintain the order of each program block which is needed for calculating the relative start locations of each block
  • Constructor Details

    • AbstractStatementBuilder

      protected AbstractStatementBuilder()
      Default constructor that initializes each variable
  • Method Details

    • getStart

      public HexNum getStart()
      Retrieves the starting address as a HexNum for the default starting block
      Returns:
      the starting address as a HexNum for the default block.
    • getStart

      public HexNum getStart(String block)
      Retrieves the starting HexNum for the specified block.
      Parameters:
      block - the name of the block for which to retrieve the starting HexNum
      Returns:
      the starting HexNum associated with the specified block, or error if the block is not found
    • getTotalLength

      public HexNum getTotalLength()
      Calculates the total length of all program blocks by summing up the location counters of each block in the start table.
      Returns:
      a HexNum representing the total length of all program blocks.
    • getLocctr

      public HexNum getLocctr()
      Retrieves the location counter (LOCCTR) for the current block.
      Returns:
      the location counter (LOCCTR) as a HexNum object.
    • getLocctr

      public HexNum getLocctr(String block)
      Retrieves the location counter (LOCCTR) for the specified block.
      Parameters:
      block - the name of the block for which to retrieve the LOCCTR
      Returns:
      the location counter (LOCCTR) as a HexNum object for the specified block, or null if the block is not found in the locctrTable
    • getStatements

      public Queue<Statement> getStatements()
      Calculates the length and relative start of each block in the program, modifies the value of each symbol to be relative to the start of the program, and returns the queue of statements. This is to be ran after all of the statements are fed into the builder

      This method performs the following steps:

      1. Calculates the starting address of each block and updates the start of the block to the sum of the total and starting address. It also updates the total with the locctr of the block.
      2. Modifies the value of each symbol to be relative to the start of the program instead of the start of their individual block, unless the symbol is absolute.
      Returns:
      a queue of statements.
    • getName

      public String getName()
      Retrieves the name, ensuring it is exactly six characters long. If the name is empty, it defaults to "OUTPUT".
      Returns:
      A string representing the name, guaranteed to be six characters long.
    • setName

      public void setName(String name)
      Sets the name for this program.
      Parameters:
      name - the name to set
    • getExternalDefinitions

      public Queue<String> getExternalDefinitions()
      Retrieves the queue of external definitions.
      Returns:
      a Queue containing the external definitions.
    • getExternalReferences

      public Queue<String> getExternalReferences()
      Retrieves the queue of external references.
      Returns:
      a Queue containing the external references.
    • getReferenceModifications

      public List<String> getReferenceModifications()
      Retrieves the list of reference modifications.
      Returns:
      a list of strings representing the reference modifications.
    • setStart

      protected void setStart(String block, HexNum start)
      Sets the starting address for a given block in the start table.
      Parameters:
      block - the name of the block
      start - the starting address as a HexNum object
    • addLocctr

      protected void addLocctr(HexNum locctr)
      Adds a given value to the location counter (locctr) of the current block.
      Parameters:
      locctr - the location counter to be added
    • addLocctr

      protected void addLocctr(String block, HexNum locctr)
      Adds the specified location counter (locctr) value to the current location counter for the given block in the locctrTable.
      Parameters:
      block - the block identifier for which the locctr is to be updated
      locctr - the HexNum value to be added to the current locctr of the specified block
    • loadInstructions

      protected final void loadInstructions(String filename)
      Loads the instruction set from a specified file and populates the instruction and format tables.

      The file should contain lines in the format: Mnemonic, Format, Opcode. Each line is split into parts where the first part is the mnemonic, the second part is the format, and the third part is the opcode.

      The method reads the file from within the JAR using Class.getResourceAsStream(String).

      The format is mapped to an enum Format and stored in the format table. The opcode is converted to a HexNum and stored in the instruction table.

      If an unexpected format is encountered, it defaults to Format.ASM and logs a warning. If the file cannot be found or read, it logs a warning with the filename.

      Parameters:
      filename - the name of the file containing the instruction set
    • loadRegisters

      protected final void loadRegisters(String filename)
      Loads the registers from the specified file and adds them to the register table. The file should contain register names and their corresponding hexadecimal values separated by whitespace on each line. The file is expected to be in the format: Register letter, Hex Value It is very similar in steps to loadInstructions
      Parameters:
      filename - the name of the file containing the register data
      See Also:
    • splitStatement

      protected String[] splitStatement(String statement) throws InvalidAssemblyFileException
      Splits an assembly statement into its constituent parts: mnemonic, arguments, and label.
      Parameters:
      statement - the assembly statement to be split
      Returns:
      a String array containing the mnemonic, arguments, and label in that order
      Throws:
      InvalidAssemblyFileException - if the statement has an invalid number of arguments
    • handleModification

      protected String handleModification(String copyArgs, String part)
      Handles the modification of an external reference in the assembly code. If the part is an external reference, it sets the value to 0 and adds a modification record because the value is not known at assembly time. Format of Modification records: Modification Records (Revised) Col 1. M Col 2-7. Starting address of the field to be modified, relative to beginning of control section Col 8-9. Length of field to be modified in half-bytes Col. 10. Modification flag (+ or -) Col. 11-16. External symbol whose value is to be added or subtracted from the indicated field
      Parameters:
      copyArgs - the a copy of the provided arguments, which may include a sign indicating addition or subtraction.
      part - the label of the code that is an external reference.
      Returns:
      the modified copyArgs with the external reference handled.
    • evaluateExpression

      protected String evaluateExpression(String args)
      Evaluates a mathematical expression represented as a string. The expression can contain symbols that are either defined in the symbol table or are external references. Symbols in the expression are replaced with their corresponding values before evaluation.
      Parameters:
      args - The string representation of the mathematical expression to evaluate.
      Returns:
      The evaluated result as a string if the expression is valid and contains more than one part; otherwise, returns the original string.
    • handleExpression

      protected HexNum handleExpression(String label, String args)
      Handles the evaluation of an expression, replacing symbols with their decimal values, and determining if the expression is absolute or relative. The result is stored in the symbol table and returned as a HexNum object. Note that it is very similar to evaluateExpression. The main difference is that this detects and handles the situation when it is an absolute expression.
      Parameters:
      label - The label associated with the expression.
      args - The expression to be evaluated, potentially containing symbols and operators.
      Returns:
      The evaluated expression as a HexNum object.
    • handleLabels

      protected void handleLabels(String label, String mnemonic, String args) throws InvalidAssemblyFileException
      Handles the processing of labels in the assembly code. This method sets the label to the current location if it is not already in the symbol table and one of the following conditions is true: 1) The mnemonic is not "EQU". 2) The args is "*". If the mnemonic is "EQU", it evaluates the expression in args and adds it to the symbol table. If the label already exists in the symbol table, it throws an InvalidAssemblyFileException. Additionally, if the mnemonic is "START", it sets the name to the label for later object code generation.
      Parameters:
      label - The label to be processed.
      mnemonic - The mnemonic associated with the label.
      args - The arguments associated with the mnemonic.
      Throws:
      InvalidAssemblyFileException - If a duplicate label is found or other assembly file errors occur.
    • handleLiteral

      protected void handleLiteral(String args) throws InvalidAssemblyFileException
      Handles a literal in the assembly code. This method processes a literal by creating a DirectiveStatement, setting its directive to the provided argument, and then handling it as a BYTE statement. The processed literal is added to a queue to be handled later when LTORG is called or after the END directive.
      Parameters:
      args - the literal to be processed
      Throws:
      InvalidAssemblyFileException - if the literal cannot be processed correctly
    • handleByte

      protected void handleByte(String args, DirectiveStatement statement) throws InvalidAssemblyFileException
      Handles the BYTE directive in an assembly file.
      Parameters:
      args - The argument string for the BYTE directive. It should start with either 'C' or 'X'. 'C' represents a constant string whose length is the length of the string, and the object code is the ASCII value of each character in the string. 'X' represents an object code whose length is 1, and the object code is the argument itself.
      statement - The DirectiveStatement object to be updated with the size and object code.
      Throws:
      InvalidAssemblyFileException - If the argument is invalid or does not start with 'C' or 'X'.
    • assembleLiterals

      protected void assembleLiterals()
      Assembles literals by processing each unique literal from the literals queue. For each literal, it checks if the literal's directive is already present in the symbol table. If not, it adds the directive to the symbol table, adds the statement to the current list of statements, and updates the location counter.
    • handleAsmStatement

      protected DirectiveStatement handleAsmStatement(String mnemonic, String args) throws InvalidAssemblyFileException
      Handles assembly statements by processing the given mnemonic and arguments. Depending on the mnemonic, it performs various operations such as setting directives, evaluating expressions, managing location counters, and handling literals.
      Parameters:
      mnemonic - The assembly mnemonic to be processed.
      args - The arguments associated with the mnemonic.
      Returns:
      A DirectiveStatement object containing the processed directive and its associated data.
      Throws:
      InvalidAssemblyFileException - If the mnemonic is invalid or if there is an error in processing the statement.
    • addStatement

      protected void addStatement(Statement statement)
      Adds a statement to the list of statements. If the provided statement is not null, it sets the block and control section of the statement to the current block and name, respectively, and then adds the statement to the list of statements. Use this method to add new statements to the queue as it handles necessary data
      Parameters:
      statement - the statement to be added. If null, the method does nothing.
    • processStatement

      public abstract void processStatement(String statement) throws InvalidAssemblyFileException, ScriptException
      Processes a given assembly statement.
      Parameters:
      statement - the assembly statement to be processed
      Throws:
      InvalidAssemblyFileException - if the statement is invalid or cannot be processed
      ScriptException - if there is an error in the script processing