1 package edu.berkeley.fleet.interpreter;
4 import java.util.concurrent.*;
5 import java.lang.reflect.*;
6 import edu.berkeley.sbp.util.ANSI;
7 import edu.berkeley.fleet.api.*;
8 import edu.berkeley.fleet.two.*;
9 import edu.berkeley.fleet.assembler.*;
10 import edu.berkeley.fleet.util.*;
12 public class Interpreter extends FleetTwoFleet {
14 /** used to allocate serial numbers; see InterpreterDestination for further detail */
15 int maxAllocatedDestinationSerialNumber = 0;
17 private InterpreterShip debugShip = null;
18 private BlockingQueue<BitVector> debugStream = new LinkedBlockingQueue<BitVector>();
19 private LinkedHashMap<String,InterpreterShip> ships = new LinkedHashMap<String,InterpreterShip>();
20 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
21 public Ship getShip(String type, int ordinal) {
23 if (s.getType().equals(type))
29 /** do not use this; it is going to go away */
30 public Interpreter() { this(true); }
31 public Interpreter(boolean logging) {
66 public Interpreter(String[] ships, boolean logging) {
69 for(String s : ships) {
70 createShip(ships[i], ships[i]+"_"+i);
75 void dispatch(Instruction i) {
77 long il = writeInstruction(i, debugShip.getDock("in"));
78 Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
79 new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
82 private Ship createShip(String shipType, String shipname) {
84 if (ships.get(shipname)!=null) return ships.get(shipname);
85 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
86 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
87 String src = "/ships/" + shipType + ".ship";
88 InputStream is = getClass().getResourceAsStream(src);
89 BufferedReader br = new BufferedReader(new InputStreamReader(is));
90 ShipDescription sd = new ShipDescription(shipType, br);
91 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
92 ships.put(shipname, ret);
93 if (shipType.equals("Debug") && debugShip == null)
96 } catch (Exception e) {
102 void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
103 void debug(BitVector data) {
105 if (debugStream != null) debugStream.put(data);
106 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
107 } catch (Exception e) {
108 throw new RuntimeException(e);
112 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
114 public BitVector getDestAddr(Path path) {
115 long ret = ((InterpreterDestination)path.getDestination()).getSerialNumber();
116 BitVector sig = path.getSignal();
117 BitVector bv = new BitVector(DISPATCH_PATH.valmaskwidth+1);
120 if (sig.length() > 1) throw new RuntimeException("signal was " + sig.length() + " bits long!");
121 if (sig.length() > 0 && sig.get(0)) bv.set(bv.length()-1,true);
127 // ShipDescription //////////////////////////////////////////////////////////////////////////////
129 public void expand(ShipDescription sd) {
131 String filename = sd.getName();
132 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
133 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
134 new File(outf.getParent()).mkdirs();
135 System.err.println("writing to " + outf);
136 FileOutputStream out = new FileOutputStream(outf);
137 PrintWriter pw = new PrintWriter(out);
139 pw.println("package edu.berkeley.fleet.interpreter;");
140 pw.println("import edu.berkeley.sbp.util.ANSI;");
141 pw.println("import edu.berkeley.fleet.api.*;");
142 pw.println("import edu.berkeley.fleet.two.*;");
143 pw.println("import edu.berkeley.fleet.*;");
144 pw.println("import java.util.*;");
145 pw.println("import java.io.*;");
147 pw.println("public class "+filename+" extends InterpreterShip {");
149 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
150 pw.println(" super(fleet, sd);");
153 for(DockDescription b : sd) {
154 String name = b.getName();
155 pw.print(" InterpreterDock box_");
157 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
160 pw.println(sd.getSection("fleeterpreter"));
164 } catch (Exception e) { throw new RuntimeException(e); }
167 // Run //////////////////////////////////////////////////////////////////////////////
169 public FleetProcess run(final Instruction[] instructions) {
170 InterpreterProcess ip = initialize(instructions);
171 new Thread(ip).start();
175 public InterpreterProcess initialize(Instruction[] instr) {
176 return new InterpreterProcess(instr);
179 public class InterpreterProcess extends FleetProcess implements Runnable {
180 private Instruction[] instructions;
181 public void flush() { }
182 public void sendWord(Destination d, BitVector word) {
183 InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
184 ((InterpreterDestination)d).
185 addDataFromFabric(new Packet(path, word, false));
187 public void sendToken(Destination d) {
188 InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
189 ((InterpreterDestination)d).
190 addDataFromFabric(new Packet(path, new BitVector(getWordWidth()), true));
192 public InterpreterProcess(Instruction[] instructions) {
193 this.instructions = instructions;
194 for(Instruction i : instructions)
197 public Fleet getFleet() { return Interpreter.this; }
198 public void sendInstruction(Instruction i) { dispatch(i); }
199 public Dock getDebugInputDock() { return debugShip.getDock("in"); }
200 public BitVector recvWord() {
202 return debugStream.take();
203 } catch (Exception e) { throw new RuntimeException(e); }
205 protected void _terminate() { }
208 while(!isTerminated())
209 for(InterpreterShip ship : ships.values())
210 for(int j=0; j<10; j++)
212 for(InterpreterShip ship : ships.values())
215 } catch (Exception e) {
216 if (isTerminated()) return;
217 throw new RuntimeException(e);
221 public void step(Dock d) {
222 ((InterpreterDock)d).service();
225 public void step(Ship s) {
226 ((InterpreterShip)s).service();