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) {
67 public Interpreter(String[] ships, boolean logging) {
70 for(String s : ships) {
71 createShip(ships[i], ships[i]+"_"+i);
76 private Ship createShip(String shipType, String shipname) {
78 if (ships.get(shipname)!=null) return ships.get(shipname);
79 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
80 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
81 String src = "/ships/" + shipType + ".ship";
82 InputStream is = getClass().getResourceAsStream(src);
83 BufferedReader br = new BufferedReader(new InputStreamReader(is));
84 ShipDescription sd = new ShipDescription(this, shipType, br);
85 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
86 ships.put(shipname, ret);
87 if (shipType.equals("Debug") && debugShip == null)
90 } catch (Exception e) {
96 void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
97 void debug(BitVector data) {
99 if (debugStream != null) debugStream.put(data);
100 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
101 } catch (Exception e) {
102 throw new RuntimeException(e);
106 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
108 public BitVector getDestAddr(Path path) {
109 long ret = ((InterpreterDestination)path.getDestination()).getSerialNumber();
110 BitVector sig = path.getSignal();
111 BitVector bv = new BitVector(DISPATCH_PATH.valmaskwidth+1);
114 if (sig.length() > 1) throw new RuntimeException("signal was " + sig.length() + " bits long!");
115 if (sig.length() > 0 && sig.get(0)) bv.set(bv.length()-1,true);
121 // ShipDescription //////////////////////////////////////////////////////////////////////////////
123 public void expand(ShipDescription sd) {
125 String filename = sd.getName();
126 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
127 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
128 new File(outf.getParent()).mkdirs();
129 System.err.println("writing to " + outf);
130 FileOutputStream out = new FileOutputStream(outf);
131 PrintWriter pw = new PrintWriter(out);
133 pw.println("package edu.berkeley.fleet.interpreter;");
134 pw.println("import edu.berkeley.sbp.util.ANSI;");
135 pw.println("import edu.berkeley.fleet.api.*;");
136 pw.println("import edu.berkeley.fleet.two.*;");
137 pw.println("import edu.berkeley.fleet.*;");
138 pw.println("import java.util.*;");
139 pw.println("import java.io.*;");
141 pw.println("public class "+filename+" extends InterpreterShip {");
143 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
144 pw.println(" super(fleet, sd);");
147 for(DockDescription b : sd) {
148 String name = b.getName();
149 pw.print(" InterpreterDock box_");
151 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
154 pw.println(sd.getSection("fleeterpreter"));
158 } catch (Exception e) { throw new RuntimeException(e); }
161 // Run //////////////////////////////////////////////////////////////////////////////
163 public FleetProcess run(final Instruction[] instructions) {
164 InterpreterProcess ip = initialize(instructions);
165 Thread ipt = new Thread(ip);
171 public InterpreterProcess initialize(Instruction[] instr) {
172 return new InterpreterProcess(instr);
175 public class InterpreterProcess extends FleetProcess implements Runnable {
176 private Instruction[] instructions;
177 public synchronized void sendWord(Destination d, BitVector word) { sendWord(d, word, null); }
178 public synchronized void sendWord(Destination d, BitVector word, BitVector signal) {
179 InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, signal==null?new BitVector(1):signal);
180 ((InterpreterDestination)d).
181 addDataFromFabric(new Packet(path, word, false));
183 public synchronized void sendToken(Destination d) {
184 InterpreterPath path = (InterpreterPath)debugShip.getDock("in").getPath(d, new BitVector(1));
185 ((InterpreterDestination)d).
186 addDataFromFabric(new Packet(path, new BitVector(getWordWidth()), true));
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();