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 implements Parser.FleetWithDynamicShips {
14 private InterpreterShip debugShip = null;
15 private BlockingQueue<BitVector> debugStream = new LinkedBlockingQueue<BitVector>();
16 private HashMap<String,InterpreterShip> ships = new HashMap<String,InterpreterShip>();
17 public Iterator<Ship> iterator() { return (Iterator<Ship>)(Object)ships.values().iterator(); }
18 public Ship getShip(String type, int ordinal) {
20 if (s.getType().equals(type))
26 /** do not use this; it is going to go away */
27 public Interpreter() {
30 public Interpreter(String[] ships, boolean logging) {
33 for(String s : ships) {
34 createShip(ships[i], ships[i]+"_"+i);
39 void dispatch(Instruction i) {
41 long il = writeInstruction(i, debugShip.getDock("in"));
42 Path path = debugShip.getDock("in").getPath(i.dock.getInstructionDestination(), null);
43 new Packet((InterpreterPath)path, new BitVector(getWordWidth()).set(il), false).send();
46 /** do not use this; it is going to go away */
47 public Ship createShip(String shipType, String shipname) {
49 if (ships.get(shipname)!=null) return ships.get(shipname);
50 Class c = Class.forName("edu.berkeley.fleet.interpreter."+shipType);
51 Constructor con = c.getConstructor(new Class[] { Interpreter.class, String.class, ShipDescription.class });
52 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+shipType+".ship")));
53 ShipDescription sd = new ShipDescription(shipType, br);
54 InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
55 ships.put(shipname, ret);
56 if (shipType.equals("Debug") && debugShip == null)
59 } catch (Exception e) {
65 void debug(long d) { debug(new BitVector(getWordWidth()).set(d)); }
66 void debug(BitVector data) {
68 if (debugStream != null) debugStream.put(data);
69 else Log.println(ANSI.invert(" DEBUG: got a datum: " + data+ANSI.clreol()));
70 } catch (Exception e) {
71 throw new RuntimeException(e);
75 // Instruction Encoding /////////////////////////////////////////////////////////////////////////
77 public BitVector getDestAddr(Path path) {
78 long ret = ((InterpreterDestination)path.getDestination()).addr;
79 BitVector bv = path.getSignal();
81 if (bv.length() > 1) throw new RuntimeException("signal was " + bv.length() + " bits long!");
82 if (bv.length() > 0 && bv.get(0)) ret |= 1;
84 throw new RuntimeException("fixme");
88 // ShipDescription //////////////////////////////////////////////////////////////////////////////
90 public void expand(ShipDescription sd) {
92 String filename = sd.getName();
93 //String filename = (sd.getName().charAt(0)+"").toUpperCase() + sd.getName().substring(1).toLowerCase();
94 File outf = new File("build/java/edu/berkeley/fleet/interpreter/"+filename+".java");
95 new File(outf.getParent()).mkdirs();
96 System.err.println("writing to " + outf);
97 FileOutputStream out = new FileOutputStream(outf);
98 PrintWriter pw = new PrintWriter(out);
100 pw.println("package edu.berkeley.fleet.interpreter;");
101 pw.println("import edu.berkeley.sbp.util.ANSI;");
102 pw.println("import edu.berkeley.fleet.api.*;");
103 pw.println("import edu.berkeley.fleet.two.*;");
104 pw.println("import edu.berkeley.fleet.*;");
105 pw.println("import java.util.*;");
106 pw.println("import java.io.*;");
108 pw.println("public class "+filename+" extends InterpreterShip {");
110 for(DockDescription b : sd) {
111 String name = b.getName();
112 pw.print(" InterpreterDock box_");
114 pw.print(" = new InterpreterDock(this, shipDescription.getDockDescription(\""+name+"\"));");
117 pw.println(" public "+filename+"(Interpreter fleet, String name, ShipDescription sd) {");
118 pw.println(" super(fleet, sd);");
119 for(DockDescription b : sd)
120 pw.println(" addDock(\""+b.getName()+"\", box_"+b.getName()+");");
123 pw.println(sd.getSection("fleeterpreter"));
127 } catch (Exception e) { throw new RuntimeException(e); }
130 // Run //////////////////////////////////////////////////////////////////////////////
132 public FleetProcess run(final Instruction[] instructions) {
133 InterpreterProcess ip = new InterpreterProcess(instructions);
134 new Thread(ip).start();
138 private class InterpreterProcess extends FleetProcess implements Runnable {
139 private Instruction[] instructions;
140 public void dispatchWord(Destination d, BitVector word) {
141 throw new RuntimeException("not implemented");
143 public InterpreterProcess(Instruction[] instructions) {
144 this.instructions = instructions;
145 for(Instruction i : instructions)
146 dispatchInstruction(i);
148 public Fleet getFleet() { return Interpreter.this; }
149 public void dispatchInstruction(Instruction i) { dispatch(i); }
150 public Dock getDebugInputDock() { return debugShip.getDock("in"); }
151 public BitVector readWord() {
153 return debugStream.take();
154 } catch (Exception e) { throw new RuntimeException(e); }
156 protected void _terminate() { }
159 while(!isTerminated())
160 for(InterpreterShip ship : ships.values())
161 for(int j=0; j<10; j++)
163 for(InterpreterShip ship : ships.values())
166 } catch (Exception e) {
167 if (isTerminated()) return;
168 throw new RuntimeException(e);