Skip to content

Commit

Permalink
Implement exception-less RAW conflicts
Browse files Browse the repository at this point in the history
Profiling revealed that a lot of time in unit tests is spent throwing
and catching the RAWException exception across method boundaries.

Apparently, throwing the exception within the method is much cheaper,
hence the changes in this commit.

In this commit, the Instruction.ID() method returns true when a RAW
conflict is detected, false otherwise. While this is not a clean
interface, it makes the longest unit test (testSetBitSort) roughly
30% faster.

Also, while changing the instructions, some of the simplifications
suggested by IntelliJ were applied.

This should mitigate in part Issue #132.
  • Loading branch information
lupino3 committed Oct 8, 2017
1 parent 3b7b358 commit e9d9238
Show file tree
Hide file tree
Showing 61 changed files with 250 additions and 141 deletions.
15 changes: 12 additions & 3 deletions src/main/java/org/edumips64/core/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,10 @@ public void step() throws AddressErrorException, HaltException, IrregularWriteOp
syncex = stepEX();

// ID: instruction decode / register fetch stage
stepID();
boolean rawException = stepID();
if (rawException) {
throw new RAWException();
}

// IF: instruction fetch stage.
stepIF();
Expand Down Expand Up @@ -560,7 +563,9 @@ private void stepIF() throws IrregularStringOfBitsException, HaltException, Irre
}
}

private void stepID() throws TwosComplementSumException, WAWException, IrregularStringOfBitsException, FPInvalidOperationException, BreakException, HaltException, RAWException, IrregularWriteOperationException, JumpException, FPDividerNotAvailableException, FPFunctionalUnitNotAvailableException, EXNotAvailableException {
// Returns true if there is a RAW conflict, false otherwis3. See docs for Instruction.ID()
// for an explanation of why it is the case.
private boolean stepID() throws TwosComplementSumException, WAWException, IrregularStringOfBitsException, FPInvalidOperationException, BreakException, HaltException, RAWException, IrregularWriteOperationException, JumpException, FPDividerNotAvailableException, FPFunctionalUnitNotAvailableException, EXNotAvailableException {
changeStage(PipeStage.ID);

if (pipe.get(PipeStage.ID) != null) {
Expand All @@ -579,7 +584,10 @@ private void stepID() throws TwosComplementSumException, WAWException, Irregular

logger.info("Executing ID() for " + pipe.get(PipeStage.ID));
// Can change the CPU status from RUNNING to STOPPING.
pipe.get(PipeStage.ID).ID();
boolean rawException = pipe.get(PipeStage.ID).ID();
if (rawException) {
return true;
}

if (isFP) {
logger.info("Moving " + pipe.get(PipeStage.ID) + " to the FP pipeline.");
Expand All @@ -590,6 +598,7 @@ private void stepID() throws TwosComplementSumException, WAWException, Irregular
}
pipe.put(PipeStage.ID, null);
}
return false;
}

private String stepEX() throws SynchronousException, HaltException, NotAlignException, TwosComplementSumException, IrregularWriteOperationException, AddressErrorException, IrregularStringOfBitsException {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/edumips64/core/is/ALU_IType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.edumips64.core.is;

import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
//per diagnostica
import java.util.logging.Logger;
Expand Down Expand Up @@ -50,12 +51,12 @@ public class ALU_IType extends ComputationalInstructions {
this.paramCount = 3;
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
//if the source register is valid passing its own values into a temporary register
Register rs = cpu.getRegister(params.get(RS_FIELD));

if (rs.getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

TR[RS_FIELD].setBits(rs.getBinString(), 0);
Expand All @@ -64,7 +65,7 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula
rt.incrWriteSemaphore();
//writing the immediate value of "params" on a temporary register
TR[IMM_FIELD].writeHalf(params.get(IMM_FIELD));

return false;
}

public void EX() throws IrregularStringOfBitsException, IntegerOverflowException, TwosComplementSumException, IrregularWriteOperationException {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/edumips64/core/is/ALU_RType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
package org.edumips64.core.is;

import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
//per diagnostica
import java.util.*;
import java.util.logging.Logger;

/**This is the base class for the R-Type instructions
Expand All @@ -53,7 +53,7 @@ public abstract class ALU_RType extends ComputationalInstructions {
paramCount = 3;
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
//if source registers are valid passing their own values into temporary registers
logger.info("Executing step ID of " + fullname);
logger.info("RD is R" + params.get(RD_FIELD) + "; RS is R" + params.get(RS_FIELD) + "; RT is R" + params.get(RT_FIELD) + ";");
Expand All @@ -64,12 +64,12 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula
// throw new RAWException();
if (rs.getWriteSemaphore() > 0) {
logger.info("RAW on RS");
throw new RAWException();
return true;
}

if (rt.getWriteSemaphore() > 0) {
logger.info("RAW on RT");
throw new RAWException();
return true;
}

TR[RS_FIELD].setBits(rs.getBinString(), 0);
Expand All @@ -86,6 +86,7 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula
// Lock RD
rd.incrWriteSemaphore();
logger.info("RD = " + TR[RD_FIELD].getValue() + "; RS = " + TR[RS_FIELD].getValue() + "; RT = " + TR[RT_FIELD].getValue() + ";");
return false;
}

public void EX() throws IrregularStringOfBitsException, IntegerOverflowException, TwosComplementSumException, IrregularWriteOperationException, DivisionByZeroException {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/edumips64/core/is/ANDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;


Expand All @@ -46,12 +47,12 @@ class ANDI extends ALU_IType {
}
//since this operation is carried out with zero padding of immediate, against sign_extend(immediate) methodology
//of all others instructions in the same category, is necessary the overriding of ID method
public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
//if the source register is valid passing its own values into a temporary register
Register rs = cpu.getRegister(params.get(RS_FIELD));

if (rs.getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

TR[RS_FIELD] = rs;
Expand All @@ -69,7 +70,7 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula

sb.append(TR[IMM_FIELD].getBinString().substring(48, 64));
TR[IMM_FIELD].setBits(sb.substring(0), 0);

return false;
}
public void EX() throws IrregularStringOfBitsException {
//getting values from temporary registers
Expand All @@ -80,8 +81,8 @@ public void EX() throws IrregularStringOfBitsException {

//performing bitwise OR between immediate and rs register
for (int i = 0; i < 64; i++) {
rsbit = rs.charAt(i) == '1' ? true : false;
immbit = imm.charAt(i) == '1' ? true : false;
rsbit = rs.charAt(i) == '1';
immbit = imm.charAt(i) == '1';
resbit = rsbit && immbit;
sb.append(resbit ? '1' : '0');
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/edumips64/core/is/B.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/** <pre>
* Syntax: B offset
Expand All @@ -45,7 +46,7 @@ public class B extends FlowControl_IType {
name = "B";
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, JumpException, TwosComplementSumException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
//getting registers rs and rt
//converting offset into a signed binary value of 64 bits in length
BitSet64 bs = new BitSet64();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/edumips64/core/is/BC1F.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/** <pre>
*<pre>
Expand All @@ -45,8 +46,8 @@ public class BC1F extends FPConditionalBranchesInstructions {
super.name = NAME;
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, JumpException {
boolean condition = (cpu.getFCSRConditionCode(params.get(CC_FIELD)) == 0) ? true : false;
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
boolean condition = cpu.getFCSRConditionCode(params.get(CC_FIELD)) == 0;

//converting offset into a signed binary value of 64 bits in length
BitSet64 bs = new BitSet64();
Expand All @@ -69,6 +70,7 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula

throw new JumpException();
}
return false;
}


Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/edumips64/core/is/BC1T.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/**
*<pre>
Expand All @@ -46,8 +47,8 @@ public class BC1T extends FPConditionalBranchesInstructions {
super.name = NAME;
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, JumpException {
boolean condition = (cpu.getFCSRConditionCode(params.get(CC_FIELD)) == 1) ? true : false;
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
boolean condition = cpu.getFCSRConditionCode(params.get(CC_FIELD)) == 1;

//converting offset into a signed binary value of 64 bits in length
BitSet64 bs = new BitSet64();
Expand All @@ -70,7 +71,6 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula

throw new JumpException();
}
return false;
}


}
8 changes: 4 additions & 4 deletions src/main/java/org/edumips64/core/is/BEQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/** <pre>
* Syntax: BEQ rs, rt, offset
Expand All @@ -44,9 +45,9 @@ public class BEQ extends FlowControl_IType {
name = "BEQ";
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, JumpException, TwosComplementSumException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
if (cpu.getRegister(params.get(RS_FIELD)).getWriteSemaphore() > 0 || cpu.getRegister(params.get(RT_FIELD)).getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

//getting registers rs and rt
Expand Down Expand Up @@ -74,7 +75,6 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula

throw new JumpException();
}
return false;
}


}
8 changes: 5 additions & 3 deletions src/main/java/org/edumips64/core/is/BEQZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.edumips64.core.is;

import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;

/** <pre>
Expand All @@ -46,11 +47,11 @@ public class BEQZ extends FlowControl_IType {
name = "BEQZ";
}

public void ID()
throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, JumpException, TwosComplementSumException {
public boolean ID()
throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
//getting registers rs and rt
if (cpu.getRegister(params.get(RS_FIELD)).getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

String rs = cpu.getRegister(params.get(RS_FIELD)).getBinString();
Expand All @@ -77,6 +78,7 @@ public void ID()

throw new JumpException();
}
return false;
}
public void pack() throws IrregularStringOfBitsException {
repr.setBits(OPCODE_VALUE, OPCODE_VALUE_INIT);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/edumips64/core/is/BGEZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/** <pre>
* Syntax: BGEZ rs, offset
Expand All @@ -45,9 +46,9 @@ public class BGEZ extends FlowControl_IType {
name = "BGEZ";
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, JumpException, TwosComplementSumException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
if (cpu.getRegister(params.get(RS_FIELD)).getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

//getting register rs
Expand All @@ -74,6 +75,7 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula

throw new JumpException();
}
return false;
}
public void pack() throws IrregularStringOfBitsException {
repr.setBits(OPCODE_VALUE, OPCODE_VALUE_INIT);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/edumips64/core/is/BNE.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/** <pre>
* Syntax: BNE rt, rs, immediate
Expand All @@ -43,9 +44,9 @@ public class BNE extends FlowControl_IType {
name = "BNE";
}

public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, JumpException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
if (cpu.getRegister(params.get(RS_FIELD)).getWriteSemaphore() > 0 || cpu.getRegister(params.get(RT_FIELD)).getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

//getting registers rs and rt
Expand Down Expand Up @@ -73,7 +74,6 @@ public void ID() throws RAWException, IrregularWriteOperationException, Irregula

throw new JumpException();
}
return false;
}


}
8 changes: 5 additions & 3 deletions src/main/java/org/edumips64/core/is/BNEZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.edumips64.core.is;

import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;
/** <pre>
* Syntax: BNEZ rs, offset
Expand All @@ -44,11 +45,11 @@ public class BNEZ extends FlowControl_IType {
name = "BNEZ";
}

public void ID()
throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, JumpException, TwosComplementSumException {
public boolean ID()
throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
//getting registers rs and rt
if (cpu.getRegister(params.get(RS_FIELD)).getWriteSemaphore() > 0) {
throw new RAWException();
return true;
}

String rs = cpu.getRegister(params.get(RS_FIELD)).getBinString();
Expand All @@ -75,6 +76,7 @@ public void ID()

throw new JumpException();
}
return false;
}
public void pack() throws IrregularStringOfBitsException {

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/edumips64/core/is/BREAK.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.edumips64.core.is;
import org.edumips64.core.*;
import org.edumips64.core.fpu.FPInvalidOperationException;
import org.edumips64.utils.*;

/** *Syntax: BREAK
Expand All @@ -49,7 +50,8 @@ public void IF() throws BreakException {

throw new BreakException();
}
public void ID() throws RAWException, IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException {
public boolean ID() throws IrregularWriteOperationException, IrregularStringOfBitsException, TwosComplementSumException, HaltException, JumpException, BreakException, WAWException, FPInvalidOperationException {
return false;
}

public void EX() throws HaltException, IrregularStringOfBitsException, IntegerOverflowException, TwosComplementSumException {
Expand Down

0 comments on commit e9d9238

Please sign in to comment.