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]++
--- /dev/null
+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());
+ }
+
+}
return ret;
}
+ public void dispatchCodeBag(int descriptor) {
+ System.out.println("instr: dispatching codebag #"+descriptor);
+ Program.allCodeBags.get(descriptor).dispatch(this);
+ }
}
--- /dev/null
+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());
+ }
+ }
+
+}
--- /dev/null
+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__);
+ }
+ }
+
+}
--- /dev/null
+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);
+ }
+
+}
}
// 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 });
}
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; }
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);
}
}
+ 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;
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);
--- /dev/null
+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);
+ }
+
+}
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; }
+// 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