Skip to content

Commit

Permalink
Fixed bug in jal-equivalent instructions; various changes
Browse files Browse the repository at this point in the history
Updated exception handling, updated error messages, added debug
messages and did some minor refactoring.
  • Loading branch information
gzachos committed Jan 30, 2021
1 parent b708048 commit 5da331c
Showing 1 changed file with 59 additions and 29 deletions.
88 changes: 59 additions & 29 deletions mars/tools/StackVisualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ protected void getStackData() {
System.out.println(" [" + tableModel.getValueAt(row, frameNameColumn) + "]");
}
} catch (AddressErrorException aee) {
aee.printStackTrace(); // TODO handle?
System.err.println("getStackData(): " + formatAddress(aee.getAddress()) + " AddressErrorException");
}
}

Expand Down Expand Up @@ -705,6 +705,9 @@ private void processTextMemoryUpdate(MemoryAccessNotice notice) {
}
// printBin(notice.getValue());

boolean localRaWrittenInPrevInstr = raWrittenInPrevInstr;
raWrittenInPrevInstr = false;

try {
ProgramStatement stmnt = memInstance.getStatementNoNotify(notice.getAddress());

Expand Down Expand Up @@ -735,8 +738,7 @@ else if (isJumpInstruction(instrName) || isJumpAndLinkInstruction(instrName)) {
if (isJumpAndLinkInstruction(instrName)) {
registerNewSubroutineCall(stmnt, targetLabel);
} else if (isJumpInstruction(instrName)) {
if (raWrittenInPrevInstr == true) {
raWrittenInPrevInstr = false;
if (localRaWrittenInPrevInstr == true) {
registerNewSubroutineCall(stmnt, targetLabel);
}
}
Expand All @@ -752,7 +754,6 @@ else if (isJumpInstruction(instrName) || isJumpAndLinkInstruction(instrName)) {
}
else if (isJumpRegInstruction(instrName)) {
int targetRegister = stmnt.getOperand(R_RS_OPERAND_LIST_INDEX);
// TODO: check $ra only (?)
Register reg = RegisterFile.getRegisters()[targetRegister];
int returnAddress = reg.getValue();
// returnAddress-4 is needed as PC+4 is stored in $ra when jal is executed.
Expand All @@ -769,24 +770,26 @@ else if (isJumpRegInstruction(instrName)) {
try {
Integer rasTopAddress = ras.remove(ras.size()-1);
if (rasTopAddress.compareTo(jalStatementAddress) != 0) {
System.out.println("Mismatching return address: " + rasTopAddress + " - " + jalStatementAddress);
System.err.println("Mismatching return address: " + rasTopAddress + " - " + jalStatementAddress +
" (ras vs jal)");
}
} catch (IndexOutOfBoundsException e) {
/* Exception is thrown whenever subroutine calling instructions are back-stepped (undone)
* and again executed. Undoing the last step should not be supported!
} catch (IndexOutOfBoundsException iobe) {
/* Exception is thrown whenever:
* 1) Subroutine calling instructions are back-stepped (undone) and again executed.
* Undoing the last step should not be supported! FIXED: BackStepper is disabled.
*
* It also happens in case StackVisualizer gets disconnected while user program is executing
* and then is again connected. Fixed: Tool's disconnect button is disabled during execution/simulation
* and then again enabled at the end.
* 2) In case StackVisualizer gets disconnected while user program is executing and
* then is again connected. FIXED: Tool's disconnect button is disabled during
* execution/simulation and then again enabled at the end.
*
* It also happens when tool's Reset button is pressed while user program is executing.
* 3) When tool's Reset button is pressed while the user program is executing.
* FIXED: Removed ras and activeFunctionCallStats reset operations from reset()
*/
// e.printStackTrace();
System.err.println("Mismatching number of subroutine calls and returns.");
}
}
} catch (AddressErrorException e) {
// Suppress such warnings
// e.printStackTrace();
} catch (AddressErrorException aee) {
System.err.println("processTextMemoryUpdate(): " + formatAddress(aee.getAddress()) + " AddressErrorException");
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -803,6 +806,12 @@ private void registerNewSubroutineCall(ProgramStatement stmnt, String targetLabe
ras.add(stmnt.getAddress());
Integer count = activeFunctionCallStats.addCall(targetLabel);
frameNameToBeCreated = targetLabel + " (" + count + ")";
// System.out.println("Subroutine call from: " + stmnt.getAddress() + " to " + frameNameToBeCreated);
// System.out.print("{");
// for (int addr : ras) {
// System.out.print(formatAddress(addr) + ", ");
// }
// System.out.println("}");
}


Expand Down Expand Up @@ -888,7 +897,7 @@ private void disableBackStepper() {
return;
if (bs.enabled()) {
if (debugBackStepper)
System.err.println("Disabled BackStepper");
System.out.println("Disabled BackStepper");
bs.setEnabled(false);
disabledBackStep = true;
}
Expand All @@ -905,7 +914,7 @@ private void restoreBackStepper() {
return;
if (!bs.enabled()) {
if (debugBackStepper)
System.err.println("Enabled BackStepper");
System.out.println("Enabled BackStepper");
bs.setEnabled(true);
}
}
Expand Down Expand Up @@ -998,10 +1007,11 @@ private void printBin(int num) {

@Override
protected void reset() {
if (debugBackStepper) {
System.out.flush();
System.err.println("ResetAction");
System.err.flush();
/*
* Do not reset/clear here ras or activeFunctionCallStats.
*/
if (debug) {
System.out.println("ToolReset");
}
getStackData();
updateSpDataRowColIndex();
Expand Down Expand Up @@ -1040,29 +1050,49 @@ private int calcTableColIndex(int offsetFromEnd) {
@Override
public void update(Observable observable, Object accessNotice) {
if (observable == mars.simulator.Simulator.getInstance()) {
SimulatorNotice notice = (SimulatorNotice) accessNotice;
if (notice.getAction() == SimulatorNotice.SIMULATOR_START)
onSimulationStart();
else if (notice.getAction() == SimulatorNotice.SIMULATOR_STOP)
onSimulationEnd();
processSimulatorUpdate((SimulatorNotice) accessNotice);
} else {
super.update(observable, accessNotice);
}
}


/**
* Process a {@link SimulatorNotice} and handle {@code SIMULATOR_START} or
* {@code SIMULATOR_STOP} accordingly.
*/
private void processSimulatorUpdate(SimulatorNotice notice) {
int action = notice.getAction();
if (debug)
System.out.println("\nSimulatorNotice: " + ((action == SimulatorNotice.SIMULATOR_START) ? "Start" : "End"));
if (action == SimulatorNotice.SIMULATOR_START)
onSimulationStart();
else if (action == SimulatorNotice.SIMULATOR_STOP)
onSimulationEnd();
}


/**
* Callback method after a simulation starts.
* A simulation starts each time a Run button is pressed (stepped or not).
*/
private void onSimulationStart() {
if (!isObserving())
return;
// System.err.println("SIMULATION - END: " + inSteppedExecution());
if (VenusUI.getReset()) {
// System.err.println("SIMULATION - START: " + inSteppedExecution());
if (VenusUI.getReset()) { // GUI Reset button clicks are also handled here.
if (debug)
System.out.println("GUI registers/memory reset detected");
/*
* On registers/memory reset, clear data related to subroutine calls,
* and reset/update table data.
*/
ras.clear();
activeFunctionCallStats.reset();
resetStoredRegAndFrameNameColumns(0, numberOfRows-1);
getStackData();
updateSpDataRowColIndex();
table.repaint();
}
disableBackStepper();
connectButton.setEnabled(false);
Expand Down

0 comments on commit 5da331c

Please sign in to comment.