From: adam Date: Fri, 27 Oct 2006 12:47:49 +0000 (+0100) Subject: added support for copying moves X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=9b8224a28bee18d748f322001611fa9c732436d1;p=fleet.git added support for copying moves --- diff --git a/fleet.g b/fleet.g index 98d9b92..e18ee72 100644 --- a/fleet.g +++ b/fleet.g @@ -11,7 +11,6 @@ Comment = "//" ~[\n]* "\n" | "/*" ~[\n]* "*/" -// for some reason this causes major slowness! ws = ([\r\n ] | Comment)* -> ~[\r\n ] s = ws! Program ws! @@ -19,31 +18,26 @@ Program = Program:: (Directive ws!)* CodeBagBody -Directive = Memory:: "#memory" "{" (int +/ (ws! "," ws!)) "}" /ws - | Import:: "#import" [A-Za-z_.]++ /ws - | Ship:: "#ship" shipname ":" [A-Za-z_\.]++ /ws - - Statement = Move ((ws ";")?)! | NamedCodeBag:: name ":" "{" CodeBagBody "}" /ws -// | ^"#define" Port Port /ws - -Move = Port ^"->" Destination /ws - | Port ^"-(*)->" Destination /ws - | Port ^"-(" int ")->" Destination /ws - | LiteralMove:: int "->" Destination /ws - | LiteralCountMove:: int "-(" int ")->" Destination /ws - | CodeBagMove:: CodeBag "->" Port /ws - | CodeBagMoveX:: Port "<-" CodeBag /ws - | Port ^"<-" Port /ws - -Destination = Port +/ (ws! "," ws!) + +Move = Port ^"->" Destination /ws + | Port ^"+->" Destination /ws + | Port ^"-(*)->" Destination /ws + | Port ^"-(" int ")->" Destination /ws + | Port ^"+-(" int ")->" Destination /ws + | LiteralMove:: int "->" Destination /ws + | LiteralCountMove:: int "-(" int ")->" Destination /ws + | CodeBagMove:: CodeBag "->" Port /ws + | CodeBagMoveX:: Port "<-" CodeBag /ws + | Port ^"<-" Port /ws + +Destination = "":: Port CodeBagBody = CodeBag:: (Statement +/ ws) CodeBag = CodeBagRef:: CodeBagName | "{" CodeBagBody "}" /ws CodeBagName = name -//Port = Port:: shipname ("." portname)* Port = Port:: shipname "." portname shipname = ShipName:: name portname = PortName:: name @@ -52,3 +46,10 @@ index = "[" [0-9]+ "]" | [0-9]+ int = [\-0-9]++ +// the following are not part of the official FLEET syntax and are +// specific to Adam's interpreter. + +Directive = Memory:: "#memory" "{" (int +/ (ws! "," ws!)) "}" /ws + | Import:: "#import" [A-Za-z_.]++ /ws + | Ship:: "#ship" shipname ":" [A-Za-z_\.]++ /ws + diff --git a/src/edu/berkeley/fleet/Fleet.java b/src/edu/berkeley/fleet/Fleet.java index e78ac74..9b913ae 100644 --- a/src/edu/berkeley/fleet/Fleet.java +++ b/src/edu/berkeley/fleet/Fleet.java @@ -30,6 +30,7 @@ public class Fleet { if ((i%10)==0) Log.println(" "); Log.print(mem[i] + " "); } + Log.println(); } public Ship.Inbox getInbox(String ship, String port) { diff --git a/src/edu/berkeley/fleet/Program.java b/src/edu/berkeley/fleet/Program.java index d67d394..174c6a3 100644 --- a/src/edu/berkeley/fleet/Program.java +++ b/src/edu/berkeley/fleet/Program.java @@ -45,6 +45,9 @@ public class Program { public static @bind.as("->") Statement move(Port source, Port[] dest) { return new Move(source, dest); } + public static @bind.as("+->") Statement movex(Port source, Port[] dest) { + return new Move(source, dest, true); + } public static @bind.as("CodeBagMove") Statement cbmove(CodeBag cb, Port dest) { return new CodeBagMove(cb, new Port[] { dest }); @@ -68,6 +71,9 @@ public class Program { public static @bind.as("-(") Statement smove(Port source, String count, Port[] dest) { return new Move(source, dest, Integer.parseInt(count)); } + public static @bind.as("+-(") Statement smovex(Port source, String count, Port[] dest) { + return new Move(source, dest, Integer.parseInt(count), true); + } public static @bind.as("<-") Statement gets(Port dest, Port source) { return new Move(source, new Port[] { dest }); @@ -159,11 +165,16 @@ public class Program { Port[] dest; int count; int val; + boolean copy; public Move(Port source, Port[] dest) { this(source, dest, 1); } - public Move(Port source, Port[] dest, int count) { this(0, source, dest, count); } + public Move(Port source, Port[] dest, boolean copy) { this(source, dest, 1, copy); } + public Move(Port source, Port[] dest, int count) { this(0, source, dest, count, false); } + public Move(Port source, Port[] dest, int count, boolean copy) { this(0, source, dest, count, copy); } public Move(int val, Port[] dest) { this(val, dest, 1); } public Move(int val, Port[] dest, int count) { this(val, null, dest, count); } - public Move(int val, Port source, Port[] dest, int count) { + public Move(int val, Port source, Port[] dest, int count) { this(val, source, dest, count, false); } + public Move(int val, Port source, Port[] dest, int count, boolean copy) { + this.copy = copy; this.source = source; this.dest = dest; this.count = count; @@ -180,9 +191,9 @@ public class Program { ib.add(val); Log.println("instr: " + val + " -> " + ib); } else { - if (count==0) ob.addTokenDestination(ib); + if (count==0) ob.addTokenDestination(ib, copy); else if (count==Integer.MAX_VALUE) ob.addStandingDestination(ib); - else ob.addDestination(ib); + else ob.addDestination(ib, copy); Log.println("instr: " + ob + " -> " + ib); } } diff --git a/src/edu/berkeley/fleet/Ship.java b/src/edu/berkeley/fleet/Ship.java index b670db9..acc3cf3 100644 --- a/src/edu/berkeley/fleet/Ship.java +++ b/src/edu/berkeley/fleet/Ship.java @@ -36,8 +36,9 @@ public abstract class Ship { for(Outbox ob : outboxes.values()) { if (ob.data.isEmpty()) continue; if (!ob.destination.isEmpty()) { - int data = ob.data.remove(); Inbox destination = ob.destination.remove(); + boolean copy = ob.copy.remove(); + int data = copy ? ob.data.peek() : ob.data.remove(); destination.add(data); Log.println("data: " + ob + " ----("+data+")----> " + destination); continue; @@ -71,19 +72,22 @@ public abstract class Ship { public Inbox standingDestination; public Queue data = new LinkedList(); public Queue destination = new LinkedList(); + public Queue copy = new LinkedList(); public void add(int data) { this.data.add(data); } public void flush() { data.clear(); /* FIXME: risky */ } - public void addTokenDestination(Inbox destination) { + public void addTokenDestination(Inbox destination, boolean copy) { standingDestination = null; this.destination.add(destination); this.add(0); + this.copy.add(copy); } public void addStandingDestination(Inbox destination) { standingDestination = destination; } - public void addDestination(Inbox destination) { + public void addDestination(Inbox destination, boolean copy) { standingDestination = null; this.destination.add(destination); + this.copy.add(copy); } public boolean empty() { return data.isEmpty(); } public String toString() { return Ship.this.name+"."+name; } diff --git a/src/edu/berkeley/fleet/TokenJoinShip.java b/src/edu/berkeley/fleet/TokenJoinShip.java new file mode 100644 index 0000000..c4c3f56 --- /dev/null +++ b/src/edu/berkeley/fleet/TokenJoinShip.java @@ -0,0 +1,24 @@ +package edu.berkeley.fleet; + +import java.util.*; +import java.io.*; + +public class TokenJoinShip extends Ship { + + Inbox in1 = new Inbox("in1"); + Inbox in2 = new Inbox("in2"); + Outbox out = new Outbox("out"); + + public TokenJoinShip(Fleet fleet, String name) { + super(fleet, name); + } + + public void service() { + if (!in1.empty() && !in2.empty()) { + in1.remove(); + in2.remove(); + out.add(0); + } + } + +} diff --git a/test-copymove.fleet b/test-copymove.fleet new file mode 100644 index 0000000..2e143da --- /dev/null +++ b/test-copymove.fleet @@ -0,0 +1,36 @@ +#import edu.berkeley.fleet + +// skeleton solution to problem #1 (fill in the XXX blanks) + +#memory { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } + +#ship memread : MemReadShip +#ship memwrite1 : MemWriteShip +#ship memwrite2 : MemWriteShip +#ship bitbucket : BitBucketShip +#ship halt : HaltShip +#ship fetch : FetchShip +#ship tokenjoin : TokenJoinShip + +top: { + + 2 -> memread.count + 0 -(2)-> memread.token + 0 -> memread.addr + 1 -> memread.stride + + memread.data +-(4)-> memwrite1.data + memread.data -> memwrite1.data + memread.data +-(4)-> memwrite1.data + memread.data -> memwrite1.data + + 10 -> memwrite1.count + 5 -> memwrite1.addr + 1 -> memwrite1.stride + memwrite1.token -(9)-> bitbucket.in + memwrite1.token -> halt.in + +} + +top -> fetch.in +