1 package edu.berkeley.fleet;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.meta.*;
7 import edu.berkeley.sbp.bind.*;
8 import edu.berkeley.sbp.util.*;
11 import java.lang.reflect.*;
13 public class Program {
15 Directive[] directives;
17 public Program(Directive[] directives, CodeBag root) {
19 this.directives = directives;
21 public void configure(Fleet fleet) {
22 for(Directive directive : directives)
23 directive.configure(fleet);
27 // inner classes //////////////////////////////////////////////////////////////////////////////
29 public static @bind.as("NamedCodeBag") CodeBag codeBag(String name, Object[] statements) {
30 return codeBag(statements);
33 public static @bind.as("CodeBag") CodeBag codeBag(Object[] statements) {
34 CodeBag ret = new CodeBag();
35 for(Object s : statements) ret.add((Statement)s);
39 public static @bind.as("->") Statement move(Port source, Port[] dest) {
40 return new Move(source, dest);
43 public static @bind.as("->*") Statement smove(Port source, Port[] dest) {
44 //return new SMove(source, dest);
48 public static @bind.as(":=") Statement gets(Port dest, Port source) {
49 return new Move(source, new Port[] { dest });
52 public static @bind.as("Port") Port port(String ship, String port) {
53 return new Port(ship, port);
55 public static @bind.as("Port") Port port(String ship) {
56 return new Port(ship, null);
59 public static @bind.as("ShipName") String shipname(String name, String index) { return index==null?name:name+index; }
60 public static @bind.as("PortName") String portname(String name, String index) { return index==null?name:name+index; }
62 public static interface Source { }
63 public static interface Destination { }
64 public static interface Statement {
65 public void dispatch(Fleet fleet);
68 public static @bind.as("Program") Program program(Directive[] directives, CodeBag rootCodeBag) {
69 return new Program(directives, rootCodeBag);
72 public static abstract class Directive {
73 public void configure(Fleet fleet) {
77 public static @bind.as("Memory") MemoryDirective memory(String[] values) {
78 return new MemoryDirective(values);
81 public static class MemoryDirective extends Directive{
83 public MemoryDirective(String[] values) {
84 this.mem = new int[values.length];
85 for(int i=0; i<mem.length; i++)
86 mem[i] = Integer.parseInt(values[i]);
88 public void configure(Fleet fleet) {
93 public static class ShipDirective extends Directive {
96 public @bind.as("Ship") ShipDirective(String shipname, String classname) {
97 this.shipname = shipname;
98 this.classname = classname;
100 public void configure(Fleet fleet) {
101 for(String s : fleet.imports)
102 if (tryCreate(fleet, s))
104 throw new RuntimeException("unable to instantiate class " + classname);
106 private boolean tryCreate(Fleet fleet, String packagename) {
108 Class c = Class.forName(packagename+"."+classname);
109 Constructor con = c.getConstructor(new Class[] { Fleet.class, String.class });
110 con.newInstance(new Object[] { fleet, shipname });
112 } catch (Exception e) {
118 public static class ImportDirective extends Directive {
120 public @bind.as("Import") ImportDirective(String packagename) {
121 this.packagename = packagename;
123 public void configure(Fleet fleet) {
124 fleet.imports.add(packagename);
128 public static class Move implements Statement {
131 public Move(Port source, Port[] dest) { this.source = source; this.dest = dest; }
132 public void dispatch(Fleet fleet) {
133 Ship.Outbox ob = fleet.getOutbox(source.ship, source.port);
135 Ship.Inbox ib = fleet.getInbox(d.ship, d.port);
136 ob.addDestination(ib);
137 System.out.println("instr: " + ob + " -> " + ib);
140 public String toString() {
141 StringBuffer sb = new StringBuffer();
142 sb.append(source + " -> ");
144 for(int i=1; i<dest.length; i++) {
145 sb.append(", " + dest[i]);
147 return sb.toString();
151 public static class Port implements Source, Destination {
154 public Port(String ship, String port) {
158 public String toString() { return port==null?ship:ship+"."+port; }
161 public static class CodeBag extends ArrayList<Statement> implements Statement {
162 public void dispatch(Fleet fleet) {
163 for(Statement s : this)
166 public String toString() {
167 StringBuffer s = new StringBuffer();
168 for(Statement stmt : this) s.append("\n"+stmt);
169 return "{"+indentify(s.toString())+"\n}";
173 private static String indentify(String s) {
174 StringBuffer s2 = new StringBuffer();
175 for(int i=0; i<s.length(); i++) {
176 char c = s.charAt(i);
181 return s2.toString();