[O] Optimize code

This commit is contained in:
Hykilpikonna
2022-11-10 13:50:16 -05:00
parent 3e32e2ef88
commit d0771c3ccb
4 changed files with 25 additions and 58 deletions
@@ -380,7 +380,7 @@ public class BackStepper
// Represents a "back step" (undo action) on the stack. // Represents a "back step" (undo action) on the stack.
private class BackStep private static class BackStep
{ {
private int action; // what do do MEMORY_RESTORE_WORD, etc private int action; // what do do MEMORY_RESTORE_WORD, etc
@@ -442,7 +442,7 @@ public class BackStepper
// regardless of how many steps are executed. This will speed things up a bit // regardless of how many steps are executed. This will speed things up a bit
// and make life easier for the garbage collector. // and make life easier for the garbage collector.
private class BackstepStack private static class BackstepStack
{ {
private final int capacity; private final int capacity;
@@ -49,7 +49,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
public class ProgramArgumentList public class ProgramArgumentList
{ {
ArrayList programArgumentList; ArrayList<String> programArgumentList;
/** /**
* Constructor that parses string to produce list. Delimiters are the default Java StringTokenizer delimiters * Constructor that parses string to produce list. Delimiters are the default Java StringTokenizer delimiters
@@ -60,7 +60,7 @@ public class ProgramArgumentList
public ProgramArgumentList(String args) public ProgramArgumentList(String args)
{ {
StringTokenizer st = new StringTokenizer(args); StringTokenizer st = new StringTokenizer(args);
programArgumentList = new ArrayList(st.countTokens()); programArgumentList = new ArrayList<>(st.countTokens());
while (st.hasMoreTokens()) while (st.hasMoreTokens())
{ {
programArgumentList.add(st.nextToken()); programArgumentList.add(st.nextToken());
@@ -86,7 +86,7 @@ public class ProgramArgumentList
*/ */
public ProgramArgumentList(String[] list, int startPosition) public ProgramArgumentList(String[] list, int startPosition)
{ {
programArgumentList = new ArrayList(list.length - startPosition); programArgumentList = new ArrayList<>(list.length - startPosition);
programArgumentList.addAll(Arrays.asList(list).subList(startPosition, list.length)); programArgumentList.addAll(Arrays.asList(list).subList(startPosition, list.length));
} }
@@ -95,7 +95,7 @@ public class ProgramArgumentList
* *
* @param list ArrayList of String, each element containing one argument * @param list ArrayList of String, each element containing one argument
*/ */
public ProgramArgumentList(ArrayList list) public ProgramArgumentList(ArrayList<String> list)
{ {
this(list, 0); this(list, 0);
} }
@@ -108,15 +108,15 @@ public class ProgramArgumentList
* @param startPosition Index of array element containing the first argument; all remaining elements are assumed * @param startPosition Index of array element containing the first argument; all remaining elements are assumed
* to contain an argument. * to contain an argument.
*/ */
public ProgramArgumentList(ArrayList list, int startPosition) public ProgramArgumentList(ArrayList<? extends String> list, int startPosition)
{ {
if (list == null || list.size() < startPosition) if (list == null || list.size() < startPosition)
{ {
programArgumentList = new ArrayList(0); programArgumentList = new ArrayList<>(0);
} }
else else
{ {
programArgumentList = new ArrayList(list.size() - startPosition); programArgumentList = new ArrayList<>(list.size() - startPosition);
for (int i = startPosition; i < list.size(); i++) for (int i = startPosition; i < list.size(); i++)
{ {
programArgumentList.add(list.get(i)); programArgumentList.add(list.get(i));
@@ -166,10 +166,11 @@ public class ProgramArgumentList
String programArgument; String programArgument;
int[] argStartAddress = new int[programArgumentList.size()]; int[] argStartAddress = new int[programArgumentList.size()];
try try
{ // needed for all memory writes {
// needed for all memory writes
for (int i = 0; i < programArgumentList.size(); i++) for (int i = 0; i < programArgumentList.size(); i++)
{ {
programArgument = (String) programArgumentList.get(i); programArgument = programArgumentList.get(i);
Globals.memory.set(highAddress, 0, 1); // trailing null byte for each argument Globals.memory.set(highAddress, 0, 1); // trailing null byte for each argument
highAddress--; highAddress--;
for (int j = programArgument.length() - 1; j >= 0; j--) for (int j = programArgument.length() - 1; j >= 0; j--)
@@ -206,9 +207,6 @@ public class ProgramArgumentList
RegisterFile.getUserRegister("$sp").setValue(stackAddress + Memory.WORD_LENGTH_BYTES); RegisterFile.getUserRegister("$sp").setValue(stackAddress + Memory.WORD_LENGTH_BYTES);
RegisterFile.getUserRegister("$a0").setValue(argStartAddress.length); // argc RegisterFile.getUserRegister("$a0").setValue(argStartAddress.length); // argc
RegisterFile.getUserRegister("$a1").setValue(stackAddress + Memory.WORD_LENGTH_BYTES + Memory.WORD_LENGTH_BYTES); // argv RegisterFile.getUserRegister("$a1").setValue(stackAddress + Memory.WORD_LENGTH_BYTES + Memory.WORD_LENGTH_BYTES); // argv
//RegisterFile.updateRegister("$sp",stackAddress+Memory.WORD_LENGTH_BYTES);
//RegisterFile.updateRegister("$a0",argStartAddress.length); // argc
//RegisterFile.updateRegister("$a1",stackAddress+Memory.WORD_LENGTH_BYTES+Memory.WORD_LENGTH_BYTES); // argv
} }
catch (AddressErrorException aee) catch (AddressErrorException aee)
{ {
@@ -280,44 +280,27 @@ public abstract class AbstractMarsToolAndApplication extends JFrame implements M
connectButton = new ConnectButton(); connectButton = new ConnectButton();
connectButton.setToolTipText("Control whether tool will respond to running MIPS program"); connectButton.setToolTipText("Control whether tool will respond to running MIPS program");
connectButton.addActionListener( connectButton.addActionListener(
new ActionListener() e ->
{ {
public void actionPerformed(ActionEvent e) if (connectButton.isConnected())
{ {
if (connectButton.isConnected()) connectButton.disconnect();
{ }
connectButton.disconnect(); else
} {
else connectButton.connect();
{
connectButton.connect();
}
} }
}); });
connectButton.addKeyListener(new EnterKeyListener(connectButton)); connectButton.addKeyListener(new EnterKeyListener(connectButton));
JButton resetButton = new JButton("Reset"); JButton resetButton = new JButton("Reset");
resetButton.setToolTipText("Reset all counters and other structures"); resetButton.setToolTipText("Reset all counters and other structures");
resetButton.addActionListener( resetButton.addActionListener(e -> reset());
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
resetButton.addKeyListener(new EnterKeyListener(resetButton)); resetButton.addKeyListener(new EnterKeyListener(resetButton));
JButton closeButton = new JButton("Close"); JButton closeButton = new JButton("Close");
closeButton.setToolTipText("Close (exit) this tool"); closeButton.setToolTipText("Close (exit) this tool");
closeButton.addActionListener( closeButton.addActionListener(e -> performToolClosingDuties());
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
performToolClosingDuties();
}
});
closeButton.addKeyListener(new EnterKeyListener(closeButton)); closeButton.addKeyListener(new EnterKeyListener(closeButton));
// Add all the buttons... // Add all the buttons...
@@ -441,26 +424,12 @@ public abstract class AbstractMarsToolAndApplication extends JFrame implements M
JButton resetButton = new JButton("Reset"); JButton resetButton = new JButton("Reset");
resetButton.setToolTipText("Reset all counters and other structures"); resetButton.setToolTipText("Reset all counters and other structures");
resetButton.addActionListener( resetButton.addActionListener(e -> reset());
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
resetButton.addKeyListener(new EnterKeyListener(resetButton)); resetButton.addKeyListener(new EnterKeyListener(resetButton));
JButton closeButton = new JButton("Exit"); JButton closeButton = new JButton("Exit");
closeButton.setToolTipText("Exit this application"); closeButton.setToolTipText("Exit this application");
closeButton.addActionListener( closeButton.addActionListener(e -> performAppClosingDuties());
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
performAppClosingDuties();
}
});
closeButton.addKeyListener(new EnterKeyListener(closeButton)); closeButton.addKeyListener(new EnterKeyListener(closeButton));
@@ -735,7 +704,7 @@ public abstract class AbstractMarsToolAndApplication extends JFrame implements M
// are attached to the button at the time of the call. Otherwise, // are attached to the button at the time of the call. Otherwise,
// it will call actionPerformed for the first action listener in the // it will call actionPerformed for the first action listener in the
// button's list. // button's list.
protected class EnterKeyListener extends KeyAdapter protected static class EnterKeyListener extends KeyAdapter
{ {
AbstractButton myButton; AbstractButton myButton;
@@ -840,7 +840,7 @@ public class KeyboardAndDisplaySimulator extends AbstractMarsToolAndApplication
// NOTE: last argument TRUE means update only the MMIO Control register; FALSE means update both Control and Data. // NOTE: last argument TRUE means update only the MMIO Control register; FALSE means update both Control and Data.
private synchronized void updateMMIOControlAndData(int controlAddr, int controlValue, int dataAddr, int dataValue, boolean controlOnly) private synchronized void updateMMIOControlAndData(int controlAddr, int controlValue, int dataAddr, int dataValue, boolean controlOnly)
{ {
if (!this.isBeingUsedAsAMarsTool || (this.isBeingUsedAsAMarsTool && connectButton.isConnected())) if (!this.isBeingUsedAsAMarsTool || connectButton.isConnected())
{ {
synchronized (Globals.memoryAndRegistersLock) synchronized (Globals.memoryAndRegistersLock)
{ {