Package edu.iu.jrsalata
Class AbstractStatementBuilder
java.lang.Object
edu.iu.jrsalata.AbstractStatementBuilder
- Direct Known Subclasses:
SicStatementBuilder
,StatementBuilder
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
Modifier and TypeFieldDescriptionabsoluteExpressions 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 expressionprotected String
Current name of the program blockexternalDefinitions refers to any label that may be accessed in a separate control sectionexternalReferences refers to labels in this control section that are accessed in a separate sectionformatTable is a map that holds the format of each instructioninstructionTable is a map that holds each of the assembler instructionsprotected String
Current line the builder is processingprotected int
Current line number based on given input May be inaccurate due to how comments are handledprotected Queue<DirectiveStatement>
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 herelocctrTable holds the location counter of each program blockprotected String
Name of the given programwith every external reference, we need a new modification record in order to specify where to find the external referenceregisterTable is a map that holds each valid registerprotected 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 blockstatements 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
ModifierConstructorDescriptionprotected
Default constructor that initializes each variable -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Adds a given value to the location counter (locctr) of the current block.protected void
Adds the specified location counter (locctr) value to the current location counter for the given block in the locctrTable.protected void
addStatement
(Statement statement) Adds a statement to the list of statements.protected void
Assembles literals by processing each unique literal from the literals queue.protected String
evaluateExpression
(String args) 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.getName()
Retrieves the name, ensuring it is exactly six characters long.Retrieves the list of reference modifications.getStart()
Retrieves the starting address as a HexNum for the default starting blockRetrieves 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.protected DirectiveStatement
handleAsmStatement
(String mnemonic, String args) Handles assembly statements by processing the given mnemonic and arguments.protected void
handleByte
(String args, DirectiveStatement statement) Handles the BYTE directive in an assembly file.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.protected void
handleLabels
(String label, String mnemonic, String args) Handles the processing of labels in the assembly code.protected void
handleLiteral
(String args) 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
loadInstructions
(String filename) Loads the instruction set from a specified file and populates the instruction and format tables.protected final void
loadRegisters
(String filename) Loads the registers from the specified file and adds them to the register table.abstract void
processStatement
(String statement) Processes a given assembly statement.void
Sets the name for this program.protected void
Sets the starting address for a given block in the start table.protected String[]
splitStatement
(String statement) Splits an assembly statement into its constituent parts: mnemonic, arguments, and label.
-
Field Details
-
name
Name of the given program -
block
Current name of the program block -
line
Current line the builder is processing -
lineNum
protected int lineNumCurrent line number based on given input May be inaccurate due to how comments are handled -
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
externalDefinitions refers to any label that may be accessed in a separate control section -
externalReferences
externalReferences refers to labels in this control section that are accessed in a separate section -
referenceModifications
with every external reference, we need a new modification record in order to specify where to find the external reference -
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
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
instructionTable is a map that holds each of the assembler instructions -
formatTable
formatTable is a map that holds the format of each instruction -
registerTable
registerTable is a map that holds each valid register -
locctrTable
locctrTable holds the location counter of each program block -
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
Retrieves the starting address as a HexNum for the default starting block- Returns:
- the starting address as a HexNum for the default block.
-
getStart
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
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
Retrieves the location counter (LOCCTR) for the current block.- Returns:
- the location counter (LOCCTR) as a HexNum object.
-
getLocctr
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
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 builderThis method performs the following steps:
- 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.
- 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
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
Sets the name for this program.- Parameters:
name
- the name to set
-
getExternalDefinitions
Retrieves the queue of external definitions.- Returns:
- a Queue containing the external definitions.
-
getExternalReferences
Retrieves the queue of external references.- Returns:
- a Queue containing the external references.
-
getReferenceModifications
Retrieves the list of reference modifications.- Returns:
- a list of strings representing the reference modifications.
-
setStart
Sets the starting address for a given block in the start table.- Parameters:
block
- the name of the blockstart
- the starting address as a HexNum object
-
addLocctr
Adds a given value to the location counter (locctr) of the current block.- Parameters:
locctr
- the location counter to be added
-
addLocctr
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 updatedlocctr
- the HexNum value to be added to the current locctr of the specified block
-
loadInstructions
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 aHexNum
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
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
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
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
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
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
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
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 processedScriptException
- if there is an error in the script processing
-