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) {
69 public Interpreter(String[] ships, boolean logging) {
72 for(String s : ships) {
73 createShip(ships[i], ships[i]+"_"+i);
78 private Ship createShip(String shipType, String shipname) {
80 if (ships.get(shipname)!=null) return ships.get(shipname);
81 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
82 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
83 String src = "/ships/" + shipType + ".ship";
84 InputStream is = getClass().getResourceAsStream(src);
85 BufferedReader br = new BufferedReader(new InputStreamReader(is));
86 ShipDescription sd = new ShipDescription(this, shipType, br);
87 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
88 ships.put(shipname, ret);
89 if (shipType.equals("Debug") && debugShip == null)
92 } catch (Exception e) {
98 void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
99 void debug(BitVector data) {
101 if (debugStream != null) debugStream.put(data);
102 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
103 } catch (Exception e) {
104 throw new RuntimeException(e);
108 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
110 public BitVector getDestAddr(Path path) {
111 long ret = ((InterpreterDestination)path.getDestination()).getSerialNumber();
112 BitVector sig = path.getSignal();
113 BitVector bv = new BitVector(DISPATCH_PATH.valmaskwidth+1);
116 if (sig.length() > 1) throw new RuntimeException("signal was " + sig.length() + " bits long!");
117 if (sig.length() > 0 && sig.get(0)) bv.set(bv.length()-1,true);
123 // ShipDescription //////////////////////////////////////////////////////////////////////////////
125 public void expand(ShipDescription sd) {
127 String filename = sd.getName();
128 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
129 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
130 new File(outf.getParent()).mkdirs();
131 System.err.println("writing to " + outf);
132 FileOutputStream out = new FileOutputStream(outf);
133 PrintWriter pw = new PrintWriter(out);
135 pw.println("package edu.berkeley.fleet.interpreter;");
136 pw.println("import edu.berkeley.sbp.util.ANSI;");
137 pw.println("import edu.berkeley.fleet.api.*;");
138 pw.println("import edu.berkeley.fleet.two.*;");
139 pw.println("import edu.berkeley.fleet.*;");
140 pw.println("import java.util.*;");
141 pw.println("import java.io.*;");
143 pw.println("public class "+filename+" extends InterpreterShip {");
145 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
146 pw.println(" super(fleet, sd);");
149 for(DockDescription b : sd) {
150 String name = b.getName();
151 pw.print(" InterpreterDock box_");
153 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
156 pw.println(sd.getSection("fleeterpreter"));
160 } catch (Exception e) { throw new RuntimeException(e); }
163 // Run //////////////////////////////////////////////////////////////////////////////
165 public FleetProcess run(final Instruction[] instructions) {
166 InterpreterProcess ip = initialize(instructions);
167 Thread ipt = new Thread(ip);
173 public InterpreterProcess initialize(Instruction[] instr) {
174 return new InterpreterProcess(instr);
177 public class InterpreterProcess extends FleetProcess implements Runnable {
178 private Instruction[] instructions;
179 public synchronized void sendWord(Destination d, BitVector word) { sendWord(d, word, null); }
180 public synchronized void sendWord(Destination d, BitVector word, BitVector signal) {
181 InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, signal==null?new BitVector(1):signal);
182 new Packet(path, word, false).send();
184 public synchronized void sendToken(Destination d) {
185 InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
186 new Packet(path, new BitVector(getWordWidth()), true).send();
188 public InterpreterProcess(Instruction[] instructions) {
189 this.instructions = instructions;
190 for(Instruction i : instructions)
193 public Fleet getFleet() { return Interpreter.this; }
194 public synchronized void sendInstruction(Instruction i) {
195 long il = writeInstruction(i, debugShip.getDock("in"));
196 Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
197 new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
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()) {
211 for(InterpreterShip ship : ships.values())
214 } catch (Exception e) {
215 if (isTerminated()) return;
216 throw new RuntimeException(e);
220 public void flush() {
221 // FIXME: should this run until we detect some sort of "quiescence"? OTOH that might never happen.
222 for(InterpreterShip ship : ships.values())
223 for(int j=0; j<10; j++)
230 public synchronized void step(Dock d) {
231 ((InterpreterDock)d).service();
234 public synchronized void step(Ship s) {
235 ((InterpreterShip)s).service();