first pass at getting mergesort to run on the interpreter
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterDock.java
index 9dad01b..14fd5b3 100644 (file)
@@ -23,6 +23,10 @@ class InterpreterDock extends FleetTwoDock {
 
     Queue<Instruction> instructions = new LinkedList<Instruction>();
     Queue<Packet> dataPackets = new LinkedList<Packet>();
+
+    // HACK
+    private Queue<Instruction> instructionsBackedUpIntoSwitchFabric = new LinkedList<Instruction>();
+
     boolean dataReadyForShip = false;
     boolean readyForDataFromShip = true;
     long dataFromShip;
@@ -41,6 +45,7 @@ class InterpreterDock extends FleetTwoDock {
         requeueStageHasTailInstruction = false;
         instructions.clear();
         dataPackets.clear();
+        instructionsBackedUpIntoSwitchFabric.clear();
         dataReadyForShip = false;
         readyForDataFromShip = true;
         torpedoWaiting = false;
@@ -54,6 +59,8 @@ class InterpreterDock extends FleetTwoDock {
             public String toString() { return getDock()+":i"; }
             public void addDataFromFabric(Packet p) {
                 if (p.isToken()) {
+                    if (instructionsBackedUpIntoSwitchFabric.size()!=0)
+                        throw new RuntimeException("torpedo arrived while instructions were backed up into switch fabric");
                     if (torpedoWaiting) throw new RuntimeException("two torpedoes collided!");
                     torpedoWaiting = true;
                     return;
@@ -62,28 +69,7 @@ class InterpreterDock extends FleetTwoDock {
                 Instruction inst =
                     getInterpreter().decodeInstruction(p.getValue(),
                                                        InterpreterDock.this /* this is wrong, but harmless */);
-
-                // FIXME: this is a bit too conservative.  In theory,
-                // it's okay to dispatch new instructions to the dock
-                // as long as we know that it will reach the updating
-                // state without any further input from the outside
-                // world -- ie that the instructions which remain to
-                // be executed before the requeue stage transitions to
-                // the updating state are all "non-blocking" (no moves
-                // with Ti=1 or Di=1)
-                if (requeueStageInCirculatingState || requeueStageHasTailInstruction)
-                    throw new RuntimeException("An instruction arrived while the requeue stage was circulating -- "+
-                                               "this means that instructions have backed up into "+
-                                               "the switch fabric, which nearly always risks deadlock! "+
-                                               "Fix your program!"+
-                                               "\n  dock: " + InterpreterDock.this +
-                                               "\n  instruction: " + inst
-                                               );
-                if (inst instanceof Instruction.Tail) {
-                    requeueStageHasTailInstruction = true;
-                    return;
-                }
-                instructions.add(inst);
+                addInstruction(inst);
             }
         };
     public InterpreterDestination dataDestination = new InterpreterDestination(this) {
@@ -102,6 +88,21 @@ class InterpreterDock extends FleetTwoDock {
     public int getInstructionFifoSize() { return Integer.MAX_VALUE; }
     public Interpreter getInterpreter() { return ((InterpreterShip)getShip()).getInterpreter(); }
 
+    boolean trace = false;
+
+    private void addInstruction(Instruction inst) {
+        if (requeueStageInCirculatingState || requeueStageHasTailInstruction) {
+            // GROSS HACK
+            instructionsBackedUpIntoSwitchFabric.add(inst);
+            return;
+        }
+        if (inst instanceof Instruction.Tail) {
+            requeueStageHasTailInstruction = true;
+            return;
+        }
+        instructions.add(inst);
+    }
+    
     protected final void service() {
 
         if (dataReadyForShip || flushing) return;
@@ -152,7 +153,7 @@ class InterpreterDock extends FleetTwoDock {
                     } else {
                         bv = new BitVector(getInterpreter().getWordWidth()).set(dataFromShip);
                         readyForDataFromShip = true;
-                        flag_c = flagCFromShip;
+                        if (move.latchData) flag_c = flagCFromShip;
                     }
                     if (move.latchData) dataLatch.set(bv);
                     if (move.latchPath) {
@@ -173,6 +174,12 @@ class InterpreterDock extends FleetTwoDock {
 
             } else if (instructions.peek() instanceof Instruction.Abort) {
                 requeueStageInCirculatingState = false;
+                // HACK
+                while (instructionsBackedUpIntoSwitchFabric.size()!=0) {
+                    Instruction i = instructionsBackedUpIntoSwitchFabric.remove();
+                    addInstruction(i);
+                    instructionsBackedUpIntoSwitchFabric.clear();
+                }
                 break;
 
             } else if (instructions.peek() instanceof Instruction.Flush) {
@@ -255,12 +262,7 @@ class InterpreterDock extends FleetTwoDock {
     protected long peekDataForShip() {
         if (!dataReadyForShip)
             throw new RuntimeException("peekDataForShip() invoked when dataReadyForShip()==false");
-        BitVector bv = dataLatch;
-        long val = 0;
-        for(int i=0; i<bv.length(); i++)
-            if (bv.get(i))
-                val |= (1L << i);
-        return val;
+        return dataLatch.toLong();
     }
     protected void addDataFromShip(long data) { addDataFromShip(data, false); }
     protected void addDataFromShip(long data, boolean pending_flag_c) {