added Tommy Kho memory patch
authoradam <adam@megacz.com>
Mon, 4 Dec 2006 03:22:39 +0000 (04:22 +0100)
committeradam <adam@megacz.com>
Mon, 4 Dec 2006 03:22:39 +0000 (04:22 +0100)
flow-control-example.fleet
src/edu/berkeley/fleet/Fleet.java
src/edu/berkeley/fleet/FleetParser.java
src/edu/berkeley/fleet/Log.java
src/edu/berkeley/fleet/ships/MemoryWriteShip.java [new file with mode: 0644]

index be44680..d1c23be 100644 (file)
@@ -1,6 +1,6 @@
 #import edu.berkeley.fleet.ships
 
-#ship math        : ArithmeticShip
+//#ship math        : ArithmeticShip
 #ship helper      : FifoShip
 #ship source      : FifoShip
 #ship dest        : FifoShip
@@ -11,7 +11,7 @@
 // NOTE: "accept" is a synonym for "move" it is less confusing in the case
 //       of inboxes, but is otherwise identical
 
-"foo" -> math.din
+//"foo" -> math.din
 
 //////////////////////////////////////////////////////////////////////////////
 // The following three instructions simply produce one hundred "3"s
index 7a9cc9e..2c570be 100644 (file)
@@ -39,13 +39,24 @@ public class Fleet {
     }
 
     public void dumpMem() {
+        Log.print(Log.cyan("  MEMORY: "));
         for(int i=0; i<mem.length; i++) {
-            if ((i%10)==0) Log.println("    ");
-            Log.print(mem[i] + " ");
+            if ((i%10)==0 && i!=0) Log.print(Log.cyan("          "));
+            Log.print(Log.cyan(mem[i] + " "));
+            if ((i%10)==9 && i!=mem.length-1) Log.println("");
         }
         Log.println();
     }
 
+    public void writeMem(int addr, int data) {
+        if (addr >= mem.length) {
+            int[] mem2 = new int[addr*2+1];
+            System.arraycopy(mem, 0, mem2, 0, mem2.length);
+            mem = mem2;
+        }
+        mem[addr] = data;
+    }
+
     public Ship getShip(String name) {
         Ship s = ships.get(name);
         if (s == null) throw new RuntimeException("unknown ship \""+name+"\"");
index 5132605..78934e3 100644 (file)
@@ -10,6 +10,10 @@ import java.util.*;
 import java.io.*;
 import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
 
+/**
+ *  @author Adam Megacz <megacz@cs.berkeley.edu>
+ *  @author Thomas Kho <tkho@eecs.berkeley.edu>
+ */
 public class FleetParser {
 
     public static void main(String[] s) throws Exception {
@@ -33,11 +37,21 @@ public class FleetParser {
                     DataInbox.defaultInstruction = null;
                     continue;
                 }
+            } else if (s[i].startsWith("--memory=")) {
+                String val = s[i].substring(s[i].indexOf('=')+1);
+                if (val.equals("hide")) {
+                    debugMemory = false;
+                    continue;
+                } else if (val.equals("show")) {
+                    debugMemory = true;
+                    continue;
+                }
             }
             System.out.println("Fleeterpreter usage:");
             System.out.println("");
             System.out.println("  --color={on|off}");
             System.out.println("  --inboxes={configured|unconfigured}");
+            System.out.println("  --memory={hide|show}");
             System.exit(-1);
         }
         go(new InputStreamReader(System.in));
@@ -63,6 +77,7 @@ public class FleetParser {
     private Fleet fleet;
     private ArrayList<String> imports = new ArrayList<String>();
     private CodeBag rootCodeBag;
+    private static boolean debugMemory = true;
 
     public FleetParser(Fleet fleet) {
         this.fleet = fleet;
@@ -70,8 +85,11 @@ public class FleetParser {
 
     public void done() {
         if (rootCodeBag != null) {
+            if (debugMemory) { fleet.dumpMem(); }
+            System.out.println(rootCodeBag);
             rootCodeBag.dispatch(fleet);
             fleet.go();
+            if (debugMemory) { fleet.dumpMem(); }
         }
     }
 
@@ -84,7 +102,6 @@ public class FleetParser {
             CodeBag cb = new CodeBag(null, null);
             for(Tree<String> statement : t.child(1))
                 fillCodeBag(statement, cb);
-            System.out.println(cb);
             rootCodeBag = cb;
    
         } else if (head.equals("Import")) {
@@ -101,6 +118,15 @@ public class FleetParser {
                 }
             if (!good)
                 throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
+
+        } else if (head.equals("Memory")) {
+            if (fleet.mem.length != 0)
+                throw new RuntimeException("multiple memory directives found");
+            Tree<String> m = t.child(0);
+            int[] mem = new int[m.size()];
+            for(int i=0; i<mem.length; i++)
+                mem[i] = Integer.parseInt(string(m.child(i)));
+            fleet.mem = mem;
         }
     }
 
index fec2893..d6eb662 100644 (file)
@@ -42,7 +42,7 @@ public class Log {
     public static String red(Object o) { if (!ansi_color) return o+""; return "\033[31m"+o+"\033[0m"; }
     public static String green(Object o) { if (!ansi_color) return o+""; return "\033[32m"+o+"\033[0m"; }
     public static String yellow(Object o) { if (!ansi_color) return o+""; return "\033[33m"+o+"\033[0m"; }
-    public static String blue(Object o) { if (!ansi_color) return o+""; return o+""; }
+    public static String blue(Object o) { if (!ansi_color) return o+""; return "\033[34m"+o+"\033[0m"; }
     public static String purple(Object o) { if (!ansi_color) return o+""; return "\033[35m"+o+"\033[0m"; }
     public static String cyan(Object o) { if (!ansi_color) return o+""; return "\033[36m"+o+"\033[0m"; }
     public static String invert(Object o) { if (!ansi_color) return o+""; return "\033[7m"+o+"\033[0m"; }
diff --git a/src/edu/berkeley/fleet/ships/MemoryWriteShip.java b/src/edu/berkeley/fleet/ships/MemoryWriteShip.java
new file mode 100644 (file)
index 0000000..4d3edf0
--- /dev/null
@@ -0,0 +1,49 @@
+package edu.berkeley.fleet.ships;
+
+import edu.berkeley.fleet.*;
+import java.util.*;
+import java.io.*;
+
+/**
+ *  @author Thomas Kho <tkho@eecs.berkeley.edu>
+ */
+public class MemoryWriteShip extends Ship {
+
+    int _count = 0;
+    int _stride = 0;
+    int _addr = 0;
+
+    DataInbox  addr   = new DataInbox(this, "addr");
+    DataInbox  stride = new DataInbox(this, "stride");
+    DataInbox  count  = new DataInbox(this, "count");
+    DataInbox  data   = new DataInbox(this, "data");
+    TokenOutbox done  = new TokenOutbox(this, "done");
+
+    public MemoryWriteShip(Fleet fleet, String name) {
+        super(fleet, name);
+    }
+
+    public void service() {
+        if (_count > 0) {
+            if (!data.dataReadyForShip()) return;
+            if (_addr<=getFleet().mem.length)
+                getFleet().writeMem(_addr, data.removeDataForShip());
+            _count--;
+            _addr += _stride;
+            if (_count==0)
+                done.addTokenFromShip();
+        } else {
+            if (count.dataReadyForShip() &&
+                addr.dataReadyForShip() &&
+                stride.dataReadyForShip() &&
+                done.readyForTokenFromShip() &&
+                data.dataReadyForShip()) {
+
+                _count  = count.removeDataForShip();
+                _addr   = addr.removeDataForShip();
+                _stride = stride.removeDataForShip();
+            }
+        }
+    }
+
+}