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 //////////////////////////////////////////////////////////////////////////////
28 public static HashMap<String,CodeBag> namedCodeBags = new HashMap<String,CodeBag>();
30 public static @bind.as("NamedCodeBag") Statement codeBag(String name, CodeBag body) {
31 namedCodeBags.put(name, body);
32 return new NullStatement();
35 public static @bind.as("CodeBag") CodeBag codeBag(Object[] statements) {
36 CodeBagReal ret = new CodeBagReal();
37 for(Object s : statements) ret.add((Statement)s);
41 public static @bind.as("CodeBagRef") CodeBag codeBagRef(String name) {
42 return new CodeBagRef(name);
45 public static @bind.as("->") Statement move(Port source, Port[] dest) {
46 return new Move(source, dest);
49 public static @bind.as("CodeBagMove") Statement cbmove(CodeBag cb, Port dest) {
50 return new CodeBagMove(cb, new Port[] { dest });
53 public static @bind.as("CodeBagMoveX") Statement cbmovex(Port dest, CodeBag cb) {
54 return new CodeBagMove(cb, new Port[] { dest });
57 public static @bind.as("LiteralMove") Statement move(String value, Port[] dest) {
58 return new LiteralMove(Integer.parseInt(value), dest);
61 public static @bind.as("->*") Statement smove(Port source, Port[] dest) {
62 //return new SMove(source, dest);
66 public static @bind.as("<-") Statement gets(Port dest, Port source) {
67 return new Move(source, new Port[] { dest });
70 public static @bind.as("Port") Port port(String ship, String port) {
71 return new Port(ship, port);
73 public static @bind.as("Port") Port port(String ship) {
74 return new Port(ship, null);
78 public static @bind.as("ShipName") String shipname(String name, String index) { return index==null?name:name+index; }
79 public static @bind.as("PortName") String portname(String name, String index) { return index==null?name:name+index; }
81 public static interface Source { }
82 public static interface Destination { }
83 public static interface Statement {
84 public void dispatch(Fleet fleet);
86 public static class NullStatement implements Statement {
87 public void dispatch(Fleet fleet) { }
90 public static @bind.as("Program") Program program(Directive[] directives, CodeBag rootCodeBag) {
91 return new Program(directives, rootCodeBag);
94 public static abstract class Directive {
95 public void configure(Fleet fleet) {
99 public static @bind.as("Memory") MemoryDirective memory(String[] values) {
100 return new MemoryDirective(values);
103 public static class MemoryDirective extends Directive{
105 public MemoryDirective(String[] values) {
106 this.mem = new int[values.length];
107 for(int i=0; i<mem.length; i++)
108 mem[i] = Integer.parseInt(values[i]);
110 public void configure(Fleet fleet) {
115 public static class ShipDirective extends Directive {
118 public @bind.as("Ship") ShipDirective(String shipname, String classname) {
119 this.shipname = shipname;
120 this.classname = classname;
122 public void configure(Fleet fleet) {
123 for(String s : fleet.imports)
124 if (tryCreate(fleet, s))
126 throw new RuntimeException("unable to instantiate class " + classname);
128 private boolean tryCreate(Fleet fleet, String packagename) {
130 Class c = Class.forName(packagename+"."+classname);
131 Constructor con = c.getConstructor(new Class[] { Fleet.class, String.class });
132 con.newInstance(new Object[] { fleet, shipname });
134 } catch (Exception e) {
140 public static class ImportDirective extends Directive {
142 public @bind.as("Import") ImportDirective(String packagename) {
143 this.packagename = packagename;
145 public void configure(Fleet fleet) {
146 fleet.imports.add(packagename);
150 public static class Move implements Statement {
153 public Move(Port source, Port[] dest) { this.source = source; this.dest = dest; }
154 public void dispatch(Fleet fleet) {
155 Ship.Outbox ob = fleet.getOutbox(source.ship, source.port);
157 Ship.Inbox ib = fleet.getInbox(d.ship, d.port);
158 ob.addDestination(ib);
159 Log.println("instr: " + ob + " -> " + ib);
162 public String toString() {
163 StringBuffer sb = new StringBuffer();
164 sb.append(source + " -> ");
166 for(int i=1; i<dest.length; i++) {
167 sb.append(", " + dest[i]);
169 return sb.toString();
173 public static class LiteralMove implements Statement {
176 public LiteralMove(int val, Port[] dest) { this.val = val; this.dest = dest; }
177 public void dispatch(Fleet fleet) {
179 Ship.Inbox ib = fleet.getInbox(d.ship, d.port);
181 Log.println("instr: " + val + " -> " + ib);
184 public String toString() {
185 StringBuffer sb = new StringBuffer();
186 sb.append(val + " -> ");
188 for(int i=1; i<dest.length; i++) {
189 sb.append(", " + dest[i]);
191 return sb.toString();
195 public static class CodeBagMove implements Statement {
198 public CodeBagMove(CodeBag cb, Port[] dest) { this.cb = cb; this.dest = dest; }
199 public void dispatch(Fleet fleet) {
201 Ship.Inbox ib = fleet.getInbox(d.ship, d.port);
202 ib.add(cb.getIdentifier());
203 Log.println("instr: codebag #" + cb.getIdentifier() + " -> " + ib);
206 public String toString() {
207 StringBuffer sb = new StringBuffer();
208 sb.append(cb + " -> ");
210 for(int i=1; i<dest.length; i++) {
211 sb.append(", " + dest[i]);
213 return sb.toString();
217 public static class Port implements Source, Destination {
220 public Port(String ship, String port) {
224 public String toString() { return port==null?ship:ship+"."+port; }
227 public static int master_identifier = 1;
228 public static HashMap<Integer,CodeBagReal> allCodeBags = new HashMap<Integer,CodeBagReal>();
230 public interface CodeBag extends Statement {
231 public int getIdentifier();
234 public static class CodeBagRef implements CodeBag {
236 public CodeBagRef(String name) { this.name = name; }
237 public void dispatch(Fleet fleet) {
238 namedCodeBags.get(name).dispatch(fleet);
240 public int getIdentifier() {
241 return namedCodeBags.get(name).getIdentifier();
245 public static class CodeBagReal extends ArrayList<Statement> implements Statement, CodeBag {
247 public CodeBagReal() {
248 this.identifier = master_identifier++;
249 allCodeBags.put(identifier, this);
251 public int getIdentifier() {
254 public void dispatch(Fleet fleet) {
255 for(Statement s : this)
258 public String toString() {
259 StringBuffer s = new StringBuffer();
260 for(Statement stmt : this) s.append("\n"+stmt);
261 return "{"+indentify(s.toString())+"\n}";
265 private static String indentify(String s) {
266 StringBuffer s2 = new StringBuffer();
267 for(int i=0; i<s.length(); i++) {
268 char c = s.charAt(i);
273 return s2.toString();