working demo that adds 1000 to the first 4 elements of an array
authoradam <adam@megacz.com>
Fri, 15 Sep 2006 13:13:39 +0000 (14:13 +0100)
committeradam <adam@megacz.com>
Fri, 15 Sep 2006 13:13:39 +0000 (14:13 +0100)
fleet.g
src/edu/berkeley/fleet/FetchShip.java [new file with mode: 0644]
src/edu/berkeley/fleet/Fleet.java
src/edu/berkeley/fleet/GateShip.java [new file with mode: 0644]
src/edu/berkeley/fleet/IfThenElseShip.java [new file with mode: 0644]
src/edu/berkeley/fleet/LessThanShip.java [new file with mode: 0644]
src/edu/berkeley/fleet/Program.java
src/edu/berkeley/fleet/RegisterShip.java [new file with mode: 0644]
src/edu/berkeley/fleet/Ship.java
test.fleet

diff --git a/fleet.g b/fleet.g
index 965a712..2e2f96d 100644 (file)
--- a/fleet.g
+++ b/fleet.g
@@ -25,31 +25,28 @@ Directive      = Memory::  "#memory" "{" (int +/ (ws! "," ws!)) "}" /ws
 
 
 Statement      = Move ((ws ";")?)!
-               | CodeBag
+               | NamedCodeBag:: name ":" "{" CodeBagBody "}" /ws
 //             | ^"#define" Port Port                  /ws
 
-Move           = Source       ^"->"  Destination        /ws
-               | Source       ^"->*" Destination        /ws
-               | Port         ^":="  Source             /ws
+Move           =                Port     ^"->"  Destination  /ws
+               |                Port     ^"->*" Destination  /ws
+               | LiteralMove::  int       "->"  Destination  /ws
+               | CodeBagMove::  CodeBag   "->"  Port         /ws
+               | CodeBagMoveX:: Port      "<-"  CodeBag         /ws
+               |                Port     ^"<-"  Port         /ws
 
 Destination    = Port +/ (ws! "," ws!)
-Source         = Port
-               | CodeBag
 CodeBagBody    = CodeBag:: (Statement +/ ws)
-CodeBag        = NamedCodeBag::
-                     name:(name ws! ":" ws!)?
-                     "{"
-                           ws! statements:(Statement +/ ws) ws!
-                     "}"
-                     (ws! ";")?!
+CodeBag        = CodeBagRef:: CodeBagName
+               | "{" CodeBagBody "}" /ws
+CodeBagName    = name
 
 //Port           = Port::     shipname ("." portname)*
 Port           = Port::     shipname "." portname
-               | Port::     shipname
-shipname       = ShipName:: name (index?)
-portname       = PortName:: name (index?)
-name           = [A-Za-z0-9\[\]]**
+shipname       = ShipName:: name
+portname       = PortName:: name
+name           = ([A-Za-z0-9\[\]]+ &~ ([0-9]! ~[]*)) 
 index          = "[" [0-9]+ "]"
                | [0-9]+
-int            = [0-9]++
+int            = [\-0-9]++
 
diff --git a/src/edu/berkeley/fleet/FetchShip.java b/src/edu/berkeley/fleet/FetchShip.java
new file mode 100644 (file)
index 0000000..a326ab1
--- /dev/null
@@ -0,0 +1,19 @@
+package edu.berkeley.fleet;
+
+import java.util.*;
+import java.io.*;
+
+public class FetchShip extends Ship {
+
+    Inbox  in = new Inbox("in");
+
+    public FetchShip(Fleet fleet, String name) {
+        super(fleet, name);
+    }
+
+    public void service() {
+        while (!in.empty())
+            fleet.dispatchCodeBag(in.remove());
+    }
+
+}
index 323f321..8701191 100644 (file)
@@ -41,5 +41,9 @@ public class Fleet {
         return ret;
     }
 
+    public void dispatchCodeBag(int descriptor) {
+        System.out.println("instr: dispatching codebag #"+descriptor);
+        Program.allCodeBags.get(descriptor).dispatch(this);
+    }
 
 }
diff --git a/src/edu/berkeley/fleet/GateShip.java b/src/edu/berkeley/fleet/GateShip.java
new file mode 100644 (file)
index 0000000..d0ba310
--- /dev/null
@@ -0,0 +1,22 @@
+package edu.berkeley.fleet;
+
+import java.util.*;
+import java.io.*;
+
+public class GateShip extends Ship {
+
+    Inbox  codebag = new Inbox("codebag");
+    Inbox  release = new Inbox("release");
+
+    public GateShip(Fleet fleet, String name) {
+        super(fleet, name);
+    }
+
+    public void service() {
+        if (!codebag.empty() && !release.empty()) {
+            release.remove();
+            fleet.dispatchCodeBag(codebag.remove());
+        }
+    }
+
+}
diff --git a/src/edu/berkeley/fleet/IfThenElseShip.java b/src/edu/berkeley/fleet/IfThenElseShip.java
new file mode 100644 (file)
index 0000000..2ec1029
--- /dev/null
@@ -0,0 +1,27 @@
+package edu.berkeley.fleet;
+
+import java.util.*;
+import java.io.*;
+
+public class IfThenElseShip extends Ship {
+
+    Inbox if_    = new Inbox("if");
+    Inbox then_  = new Inbox("then");
+    Inbox else_  = new Inbox("else");
+
+    public IfThenElseShip(Fleet fleet, String name) {
+        super(fleet, name);
+    }
+
+    public void service() {
+        if (!if_.empty() && !then_.empty() && !else_.empty()) {
+            int if__ = if_.remove();
+            int then__ = then_.remove();
+            int else__ = else_.remove();
+
+            if (if__ != 0) fleet.dispatchCodeBag(then__);
+            else           fleet.dispatchCodeBag(else__);
+        }
+    }
+
+}
diff --git a/src/edu/berkeley/fleet/LessThanShip.java b/src/edu/berkeley/fleet/LessThanShip.java
new file mode 100644 (file)
index 0000000..8217fea
--- /dev/null
@@ -0,0 +1,21 @@
+package edu.berkeley.fleet;
+
+import java.util.*;
+import java.io.*;
+
+public class LessThanShip extends Ship {
+
+    Inbox in1  = new Inbox("in1");
+    Inbox in2  = new Inbox("in2");
+    Outbox out = new Outbox("out");
+
+    public LessThanShip(Fleet fleet, String name) {
+        super(fleet, name);
+    }
+
+    public void service() {
+        if (!in1.empty() && !in2.empty())
+            out.add(in1.remove() < in2.remove() ? 1 : 0);
+    }
+
+}
index ff19413..4b54576 100644 (file)
@@ -25,27 +25,45 @@ public class Program {
         }
 
     // inner classes //////////////////////////////////////////////////////////////////////////////
+    public static HashMap<String,CodeBag> namedCodeBags = new HashMap<String,CodeBag>();
 
-    public static @bind.as("NamedCodeBag") CodeBag codeBag(String name, Object[] statements) {
-        return codeBag(statements);
+    public static @bind.as("NamedCodeBag") Statement codeBag(String name, CodeBag body) {
+        namedCodeBags.put(name, body);
+        return new NullStatement();
     }
 
     public static @bind.as("CodeBag") CodeBag codeBag(Object[] statements) {
-        CodeBag ret = new CodeBag();
+        CodeBagReal ret = new CodeBagReal();
         for(Object s : statements) ret.add((Statement)s);
         return ret;
     }
 
+    public static @bind.as("CodeBagRef") CodeBag codeBagRef(String name) {
+        return new CodeBagRef(name);
+    }
+
     public static @bind.as("->")       Statement move(Port source, Port[] dest) {
         return new Move(source, dest);
     }
 
+    public static @bind.as("CodeBagMove") Statement cbmove(CodeBag cb, Port dest) {
+        return new CodeBagMove(cb, new Port[] { dest });
+    }
+
+    public static @bind.as("CodeBagMoveX") Statement cbmovex(Port dest, CodeBag cb) {
+        return new CodeBagMove(cb, new Port[] { dest });
+    }
+
+    public static @bind.as("LiteralMove")    Statement move(String value, Port[] dest) {
+        return new LiteralMove(Integer.parseInt(value), dest);
+    }
+
     public static @bind.as("->*")      Statement smove(Port source, Port[] dest) {
         //return new SMove(source, dest);
         return null;
     }
 
-    public static @bind.as(":=")       Statement gets(Port dest, Port source) {
+    public static @bind.as("<-")       Statement gets(Port dest, Port source) {
         return new Move(source, new Port[] { dest });
     }
 
@@ -56,6 +74,7 @@ public class Program {
         return new Port(ship, null);
     }
 
+
     public static @bind.as("ShipName") String    shipname(String name, String index) { return index==null?name:name+index; }
     public static @bind.as("PortName") String    portname(String name, String index) { return index==null?name:name+index; }
 
@@ -64,6 +83,9 @@ public class Program {
     public static interface Statement {
         public void dispatch(Fleet fleet);
     }
+    public static class NullStatement implements Statement {
+        public void dispatch(Fleet fleet) { }
+    }
 
     public static @bind.as("Program") Program program(Directive[] directives, CodeBag rootCodeBag) {
         return new Program(directives, rootCodeBag);
@@ -148,6 +170,50 @@ public class Program {
         }
     }
 
+    public static class LiteralMove implements Statement {
+        int val;
+        Port[] dest;
+        public LiteralMove(int val, Port[] dest) { this.val = val; this.dest = dest; }
+        public void dispatch(Fleet fleet) {
+            for(Port d : dest) {
+                Ship.Inbox ib = fleet.getInbox(d.ship, d.port);
+                ib.add(val);
+                System.out.println("instr: " + val + " -> " + ib);
+            }
+        }
+        public String toString() {
+            StringBuffer sb = new StringBuffer();
+            sb.append(val + " -> ");
+            sb.append(dest[0]);
+            for(int i=1; i<dest.length; i++) {
+                sb.append(", " + dest[i]);
+            }
+            return sb.toString();
+        }
+    }
+
+    public static class CodeBagMove implements Statement {
+        CodeBag cb;
+        Port[] dest;
+        public CodeBagMove(CodeBag cb, Port[] dest) { this.cb = cb; this.dest = dest; }
+        public void dispatch(Fleet fleet) {
+            for(Port d : dest) {
+                Ship.Inbox ib = fleet.getInbox(d.ship, d.port);
+                ib.add(cb.getIdentifier());
+                System.out.println("instr: codebag #" + cb.getIdentifier() + " -> " + ib);
+            }
+        }
+        public String toString() {
+            StringBuffer sb = new StringBuffer();
+            sb.append(cb + " -> ");
+            sb.append(dest[0]);
+            for(int i=1; i<dest.length; i++) {
+                sb.append(", " + dest[i]);
+            }
+            return sb.toString();
+        }
+    }
+
     public static class Port implements Source, Destination {
         String ship;
         String port;
@@ -158,7 +224,33 @@ public class Program {
         public String toString() { return port==null?ship:ship+"."+port; }
     }
 
-    public static class CodeBag extends ArrayList<Statement> implements Statement {
+    public static int master_identifier = 1;
+    public static HashMap<Integer,CodeBagReal> allCodeBags = new HashMap<Integer,CodeBagReal>();
+
+    public interface CodeBag extends Statement {
+        public int getIdentifier();
+    }
+
+    public static class CodeBagRef implements CodeBag {
+        String name;
+        public CodeBagRef(String name) { this.name = name; }
+        public void dispatch(Fleet fleet) {
+            namedCodeBags.get(name).dispatch(fleet);
+        }
+        public int getIdentifier() {
+            return namedCodeBags.get(name).getIdentifier();
+        }
+    }
+
+    public static class CodeBagReal extends ArrayList<Statement> implements Statement, CodeBag {
+        int identifier;
+        public CodeBagReal() {
+            this.identifier = master_identifier++;
+            allCodeBags.put(identifier, this);
+        }
+        public int getIdentifier() {
+            return identifier;
+        }
         public void dispatch(Fleet fleet) {
             for(Statement s : this)
                 s.dispatch(fleet);
diff --git a/src/edu/berkeley/fleet/RegisterShip.java b/src/edu/berkeley/fleet/RegisterShip.java
new file mode 100644 (file)
index 0000000..582d5a2
--- /dev/null
@@ -0,0 +1,28 @@
+package edu.berkeley.fleet;
+
+import java.util.*;
+import java.io.*;
+
+public class RegisterShip extends Ship {
+
+    Inbox  write     = new Inbox("write");
+    Outbox read      = new Outbox("read");
+    Outbox writedone = new Outbox("writedone");
+
+    int val = 0;
+
+    public RegisterShip(Fleet fleet, String name) {
+        super(fleet, name);
+    }
+
+    public void service() {
+        while (!write.empty()) {
+            val = write.remove();
+            read.flush();
+            writedone.add(0);
+        }
+        if (read.empty())
+            read.add(val);
+    }
+
+}
index 97c832a..074b04e 100644 (file)
@@ -62,6 +62,7 @@ public abstract class Ship {
         public Queue<Integer> data = new LinkedList<Integer>();
         public Queue<Inbox> destination = new LinkedList<Inbox>();
         public void add(int data) { this.data.add(data); }
+        public void flush() { data.clear(); /* FIXME: risky */ }
         public void addDestination(Inbox destination) { this.destination.add(destination); }
         public boolean empty() { return data.isEmpty(); }
         public String toString() { return Ship.this.name+"."+name; }
index 20de475..7bf925a 100644 (file)
@@ -1,17 +1,70 @@
+// lines beginning with "#" are directives to the interpreter
+// and are not part of the actual FLEET assembly code
+
+// import the ships in the Java package edu.berkeley.fleet
 
 #import edu.berkeley.fleet
 
-#ship adder : AdderShip
-#ship memread : MemReadShip
+
+// the identifier after the token is the 
+// Java class name of the ship to use
+
+#ship adder    : AdderShip
+#ship adder2   : AdderShip
+#ship memread  : MemReadShip
 #ship memwrite : MemWriteShip
-#ship one : OneProducerShip
-#ship halt : HaltShip
+#ship halt     : HaltShip
+#ship less     : LessThanShip
+#ship ifthen   : IfThenElseShip
+#ship i        : RegisterShip
+#ship fetch    : FetchShip
+#ship gate     : GateShip
+#ship gate2    : GateShip
+
+
+// define the initial contents of memory
 
 #memory { 000, 100, 200, 300, 400, 500 }
 
-one.out      -> adder.in1
-one.out      -> adder.in2
-adder.out    -> memread.addr
-memread.data -> adder.in1
-one.out      -> adder.in2
-adder.out    -> halt.in
+top: { i.read  -> less.in1
+       0       -> less.in2
+                  less.out -> ifthen.if
+                              ifthen.then <- { 0 -> halt.in }
+                              ifthen.else <- continue
+     }
+
+continue: {
+       i.read        -> memread.addr
+       i.read        -> memwrite.addr
+       memread.data  -> adder.in1
+       1000          -> adder.in2
+       adder.out     -> memwrite.data
+       memwrite.done -> gate.release
+       gate.codebag  <- write
+     }
+write: {
+  i.read        -> adder2.in1
+  -1            -> adder2.in2
+  adder2.out    -> i.write
+  i.writedone   -> gate2.release
+  gate2.codebag <- top
+}
+
+
+4   -> i.write
+i.writedone -> gate.release
+gate.codebag <- top
+
+
+
+//1             -> adder.in1
+//1             -> adder.in2
+//adder.out     -> memread.addr
+//memread.data  -> adder.in1
+//13            -> adder.in2
+//
+//adder.out        -> less.in1
+//214              -> less.in2
+//less.out         -> ifthen.if
+//{ 7 -> halt.in } -> ifthen.then
+//{ 9 -> halt.in } -> ifthen.else